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

查看:35
本文介绍了如何在 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];

任何人都可以建议对代码进行任何改进,以便可以有效地完成动画,或者是否有任何其他替代方案,例如 openGL 或 Core Animation,如果可以,任何人都可以建议这样做的示例代码.

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;

为您的图像使用 PVR 格式(对比 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;

您也可以尝试为您的图像使用较小的封装格式(即,RGB565 代替 RGB8888,每像素 16 位而不是 32 位).

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.

有关执行相同操作的基于核心动画的教程,请查看这篇文章.正如您将看到的,您将实现一些类来执行 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天全站免登陆