Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
205 views
in Technique[技术] by (71.8m points)

iphone - 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.

Question&Answers:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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).


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...