gpt4 book ai didi

iphone - 如何在 iOS 中有效地使用图像制作动画

转载 作者:可可西里 更新时间:2023-11-01 05:38:47 24 4
gpt4 key购买 nike

我目前面临崩溃问题,如何在 iOS 中制作动画而不消耗大量内存(或有效地)?

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

例如,我在这里展示了一个包含 2 个图像的示例动画。这是我当前用于制作动画的代码。

/*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,如果可以,任何人都可以建议一个示例代码来这样做。

最佳答案

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

在您的情况下,您将获得的优势是:

  1. 使用纹理图集而不是单个图像来尽可能多地节省内存;

  2. 为您的图像使用 PVR 格式(对比 PNG); PVR 是 iPhone/iPad 图形芯片的原生格式,可以节省更多内存;

  3. 您也可以尝试为您的图像使用更小的足迹格式(即,RGB565 而不是 RGB8888,每像素 16 位而不是 32 位)。

如果您认为这对您有用,请查看 this tutorial .

您可以直接使用 Open GL 或 Core Animation 来实现同样的效果,但我认为让 Cocos2d 处理底层的东西会更好。

有关执行相同操作的基于 Core Animation 的教程,请查看 this post .正如您将看到的,您将实现一些类来执行 Cocos2d 已经为您提供的功能(以及许多其他功能)。

关于iphone - 如何在 iOS 中有效地使用图像制作动画,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8112698/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com