How do I animate over 200 images in iOS?

I have over 200 images that I want to animate in about 10 seconds. I tried using animationImages by loading the images into an array and then calling the startAnimating method. This worked well in the simulator, but crashed the iPad.

I tried calling NStimer every 1/25 of a second and changing the image every time the timer fires. It was better than the previous method, it worked well in the simulator, but also crashed at the end (lags) of the animation on the iPad.

Can someone help me and tell me the perfect way to approach this issue? Thanks.

ORIGINAL CODE:

 - (void) humptyFallingAnim { NSString *filename; if (humptyImageCounter < 285) { filename = [NSString stringWithFormat:@"Humpty Animation HD1.2 png sequence/humpty_HD1.2_%d.png", humptyImageCounter]; UIImage *someImage = [UIImage imageNamed:filename]; humptyFalling.image = someImage; NSLog(@"loaded image: %d", humptyImageCounter); humptyImageCounter++; } else { NSLog(@"Timer invalidated"); [humptyTimer invalidate]; humptyTimer = nil; } } 

EDIT: some new code that doesn't work for me

 NSString *filename; if (humptyImageCounter < 285) { filename = [NSString stringWithFormat:@"Humpty Animation HD1.2 png sequence/humpty_HD1.2_%d.png", humptyImageCounter]; @autoreleasepool { UIImage *someImage = [UIImage imageWithContentsOfFile:filename]; humptyFalling.image = someImage; NSLog(@"loaded image: %d", humptyImageCounter); } humptyImageCounter++; } else { NSLog(@"Timer invalidated"); [humptyTimer invalidate]; humptyTimer = nil; } 

EDIT 2:

 -(void) motionBegan:(UIEventSubtype)motion withEvent:(UIEvent *)event { NSLog(@"You shook it!"); [self.view bringSubviewToFront:imgSnail]; if (deviceHasBeenShaken == 0) { deviceHasBeenShaken = 1; } humptyTimer = [NSTimer scheduledTimerWithTimeInterval:(1/25) target:self selector:@selector(humptyFallingAnim) userInfo:nil repeats:YES]; [self moveHumptyPosition]; } 
+4
source share
4 answers

Unfortunately, this is similar to OpenGL ES. Is the application a game?

+3
source

Memory management :).

 NSString* somePath; UIImageView *imageView; for (i=0;i<200;i++) { @autoreleasepool { UIImage *someImage = [UIImage imageWithContentsOfFile:somePath]; imageView.image = someImage; //every time this line is executed, old reference disappears and previous UIImage is discarded } } 

I mean, you need to implement autoreleasepools and not use the image (image): + (UIImage *) method, because it caches images.

UPDATE Let me explain. Your problem is memory management. Your application should use the device’s memory efficiently, but this is not really happening. I do not know how your application works, but the general idea is shown above. UPDATE 2 let me know if you use ARC or not.

UPDATE 3 sorry for my weird language, but it's late. Forgive me:)

+3
source

Try using implicit CALayer animations. Create your layers and set their images in a loop, for example:

 NSMutableArray *allLayers = [NSMutableArray array]; for (int i = 0 ; i != 200 ; i++) { CGImageRef image = ...; // Get i-th image from an image source CALayer* sub = [CALayer layer]; sub.contents = (__bridge id)image; sub.contentsRect = CGRectMake(...); // Create a rectangle based on the image size [self.layer addSublayer:sub]; [allLayers addObject:sub]; } 

Now you can start animating your layers implicitly by setting their properties:

 for (CALayer *sub in allLayers) { sub.position = CGMakePoint(...); // The position to which to move the image } 

Once you are done with the layers, remove them from the supervisor:

 for (CALayer *sub in allLayers) { [sub removeFromSuperlayer]; } 
+3
source

Use cocos2d my friend. Save yourself a headache. What you're talking about is a hundred times more efficient with OpenGLES. However, OpenGL is not complicated, and cocos2d provides a good level of abstraction over it, so you can just do it with a few lines of code.

+1
source

All Articles