dot
dot

Reputation: 2833

What is the best way to play a UIImageView animation once and remove it from memory immediately after playing it?

I'm having a hard time removing my animation from memory. What is the best way to do this?

This is my animation code:

-(IBAction)animationOneStart { 

NSMutableArray *images = [[NSMutableArray alloc] initWithCapacity:88];

for(int count = 1; count <= 88; count++)
{
NSString *fileName = [NSString stringWithFormat:@"testPic1_%03d.jpg", count];
UIImage  *frame    = [UIImage imageNamed:fileName];
[images addObject:frame];
}

loadingImageView.animationImages = images;

loadingImageView.animationDuration = 5;
loadingImageView.animationRepeatCount = 1; //Repeats indefinitely

[loadingImageView startAnimating];
[images release];

}

Thanks!

Upvotes: 1

Views: 1704

Answers (3)

Matthew Gillingham
Matthew Gillingham

Reputation: 3429

As far as I can tell, there is not a callback to know when the imageView's animation is finished. This is unfortunate, because it means that to remove the animation when the imageView is finished animating, you will need repeatedly check the imageView's isAnimating method until it returns false. The following is an example of what I mean:

You might be able to do something like this—in your original code, add the following line

[loadingImageView startAnimating];
[images release];

[self performSelector:@selector(checkIfAnimationIsFinished) withObject:nil afterDelay:4.5f];

Then create a method

-(void)checkIfAnimationIsFinished {
    if (![loadingImageView isAnimating]) {
        [loadingImageView release];
    } else {
        [self performSelector:@selector(checkIfAnimationIsFinished) withObject:nil afterDelay:0.5f];
    }
}

This was just an example, I used the values 4.5f and 0.5f because, unfortunately, the timing is not precise, it is more of a ballpark figure. 4.9f and 0.1f might work better. Regardless, the problem is that if you set the method to fire 5.0f seconds after you start the animation, it is not 100% certain that the animation will be be finished or not. So you will need to check repeatedly if it is not finished.

Anyway, this is not the cleanest solution, but unless someone else knows how to get a callback when an imageView is finished animating, I don't know of a better solution.

Upvotes: 0

Dylan Gattey
Dylan Gattey

Reputation: 1724

Don't need to release it anymore with ARC under Xcode 4.2! Update today :)

Upvotes: 1

UPT
UPT

Reputation: 1480

[images removeAllObjects];
[images release];

Upvotes: 1

Related Questions