I have over 200 images i want to animate over the space of around 10 seconds. I have tried using animationImages
by loading the images into an array and then calling the startAnimating
method. This worked okay in the simulator but crashed the iPad.
I have tried calling an NStimer
every 1/25th of a second and changing the image each time the timer fired. This performed better than the previous method, it worked well in the simulator but also crashed near the end of the (laggy) animation on the iPad.
Can someone help me out and tell me the ideal way of approaching this problem? 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 isn't working 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];
}
Use cocos2d my friend. Save yourself the headache. What you are talking about is a hundred times more efficiently done with OpenGLES. However, OpenGL is unnecessary complicated, and cocos2d provides a good abstraction layer over it so that you can just do it with a few lines of code.
Unfortunately, this sounds like a job for OpenGL ES. Is the app a game?
Try using
CALayer
implicit animation. Create your layers and set their images in a loop, like this:Now you can start animating your layers implicitly by setting their properties:
Once you are done with the layers, remove them from superview:
Memory management :).
I mean, you have to implement autoreleasepools and don't use +(UIImage *) imageNamed: method, because it caches images.
UPDATE Let me explain. Your problem is memory management related. Your app has to use device's memory efficiently but your actually doesn't. I don't know how your app works, but general idea is shown above. UPDATE 2 let me know whether you use ARC or not.
UPDATE 3 sorry for my bizarre language but it's late. Forgive me :)