How to do animations using images efficiently in iOS

How can i do animation in iOS with out consuming much memory(or efficiently) as i am currently facing crash problems?

For an single animation I am having a sequence of 100 images and each image is about 40kb;like that there are about 7 animations totalling almost 700 images.

For example here i have shown a sample animation with 2 images.This is my current code for doing animation.

/*First taking two images into an Array*/
NSArray *imageArray = [NSArray arrayWithObjects:[UIImage imageNamed:@"img1"],[UIImage imageNamed:@"img2"],nil];  
/*Creating an image view as a layer for performing the animation */
imgView = [UIImageView alloc];
[imgView initWithFrame:CGRectMake(110,245,100,100)];
/*Setting the images for performing animations*/
imgView.animationImages = imageArray;
imgView.animationDuration = 8.5 ;//delay for performing the animation
imgView.animationRepeatCount = 1;
/* ..and finally adding the animation to the current view */
[self.view addSubview:imgView];
[imgView startAnimating];
[imgView release];
[imgView stopAnimating];
imageArray = nil;
[imageArray release];

Can anyone suggest any improvement in the code so that the animations can be efficiently done or is there any other alternative like openGL or Core Animation ,if so can anyone suggest a sample code to do so.


Solution 1:

My suggestion is using Cocos2d, which is a framework based on Open GL specifically designed for games.

In your case, the advantages you would get are:

  1. using texture atlas instead of individual images to save as much memory as possible;

  2. using the PVR format for your images (vs. PNG); PVR is the native format of the iPhone/iPad graphics chip, and it will allow for more memory saving;

  3. you could also try and use a smaller footprint format for you images (i.e., RGB565 instead of RGB8888, 16 bits per pixel instead of 32).

If you think this could work for you, have a look at this tutorial.

You could do the same by using Open GL or Core Animation directly, but I think it is better letting Cocos2d deal with the low-level stuff.

For a Core Animation based tutorial for doing the same, have a look at this post. As you will see, you will implement a few classes for doing things that Cocos2d already offers you (together with many other features).

Solution 2:

The mysterycoconut link above is a good one as it avoids the "use Cocos2D" reflex post which always seems to appear when people ask a CoreGraphics animation related question on SO. The root of the issue you are running into is a poorly thought out animationImages API which consumes far too much app memory and should never be used. You can read a detailed description of the problem at Video and Memory usage on iOS devices. My own solution to the problem is described there. Basically, you want to decompress image data to a file and then blit the contents into the graphics card. This is very very fast under iOS and CoreGraphics makes use of optimized texture logic at the graphics card level that limits buffer copies so that the device CPU is used very little. With this approach it is possible to get very fast animation (45 to 60 FPS) of hundreds of images without using up all app memory using only CoreGraphics.