如何在iOS中有效地使用图像做动画 [英] How to do animations using images efficiently in iOS

查看:188
本文介绍了如何在iOS中有效地使用图像做动画的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在iOS中消耗大量内存(或高效)的动画,因为我目前面临崩溃问题?

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

对于单个动画我有一个100个图像的序列,每个图像约为40kb;像这样有约7个动画,总共近700个图像。

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.

例如,在这里我展示了一个动画2个图片的示例。这是我当前的动画代码。

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.

推荐答案

我的建议是使用Cocos2d,这是一个基于专为游戏设计的Open GL的框架。

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. 使用纹理地图集而不是单个图片尽可能节省更多内存;

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

格式为您的图像(与PNG); PVR是iPhone / iPad图形芯片的原生格式,它将允许更多的内存节省;

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;

您还可以尝试使用更小的封装格式

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.

您可以直接使用Open GL或Core Animation来做同样的事情,但我认为最好让Cocos2d处理低级

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.

对于基于Core Animation的教程,您可以查看此信息。正如你将看到的,你将实现几个类用于做Cocos2d已经提供给你的东西(以及许多其他功能)。

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

这篇关于如何在iOS中有效地使用图像做动画的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆