Animation on the iPhone - use image sequence or rotation?

1.1k views Asked by At

I am creating a basic animation for my iPhone app. I have a choice to make between 2 different types of animation. I can use this...

NSArray *myImages = [NSArray arrayWithObjects: 
[UIImage imageNamed:@"myImage1.png"], 
[UIImage imageNamed:@"myImage2.png"], 
[UIImage imageNamed:@"myImage3.png"], 
[UIImage imageNamed:@"myImage4.gif"], nil]; 

UIImageView *myAnimatedView = [UIImageView alloc]; 
[myAnimatedView initWithFrame:[self bounds]]; 
myAnimatedView.animationImages = myImages; 
myAnimatedView.animationDuration = 0.25;  
[self addSubview:myAnimatedView]; 
[myAnimatedView release];

or something like this...

    [UIView beginAnimations:nil context:NULL];
 [UIView setAnimationDuration:1.5];
 // other animations goes here
 myImage.transform = CGAffineTransformMakeRotation(M_PI*0.5);
 // other animations goes here
 [UIView commitAnimations];

I have quite a few of these parts to animate so I want to choose an option which uses the least amount of memory and runds the quickest. Any advice would be great, thanks

3

There are 3 answers

1
Robert maefs On

There are a lot of variables to account for in your question. Much of it will depend on the size and complexity of the images and the frequency with which the animation will run. My hunch is using the API provided transform will be the cheapest but the only real way to tell will be to stress test both animations and see which holds up the best in your case.

0
Peter On

I would go hybrid: Generate your still images from the animation code and save the layer off to CGImages.

0
MoDJ On

So, the root issue that you need to consider is how many frames of animation you expect to be in memory at one time. It does not matter if you use the animationImagesArray or one single huge image with all the frames in one. Holding the decoded image data in memory takes up a lot of space, you might be able to get away with it for a small number of small images, but you could easily write some code that will crash you device due to using up all the memory. See my blog post about this specific issue video-and-memory-usage-on-ios-devices. A better way to write your code is to simply not hold all the decoded frames of video (UIImages or CGImageRefs) in memory at the same time.