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];
}
Unfortunately, this sounds like a job for OpenGL ES. Is the app a game?
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 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 :)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With