ImageView动画获得更多内存

时间:2013-04-24 05:17:49

标签: objective-c memory-management ios6 uiimageview nsarray

我的申请是在ARC 我在imageView中使用图像动画,但经过一段时间后,我的应用程序因内存错误而崩溃。

我没有在日志中收到任何错误消息,但在配置文件工具中我在分配部分中收到“内存不足”消息。

我的动画的一个代码是

ImageView= [[UIImageView alloc] init];
[self.view addSubview:ImageView];
ImageView.frame=CGRectMake(0, 0, 480, 342);
ImageView.animationImages = [NSArray arrayWithObjects:
                                  [UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"image1" ofType:@"png"]],
                                  [UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"image2" ofType:@"png"]],
                                  [UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"image3" ofType:@"png"]],
                                  [UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"image4" ofType:@"png"]],
                                  [UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"image5" ofType:@"png"]],
                                  [UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"image6" ofType:@"png"]],
                                  nil];
[ImageView setAnimationRepeatCount:1];
ImageView.animationDuration =1.0;
[ImageView startAnimating];

如果我评论这个动画代码,那么应用程序运行正常但我在一个视图中需要超过5种这种类型的动画。

任何链接,教程,任何想法都会有很大的帮助...

2 个答案:

答案 0 :(得分:0)

你为什么不尝试使用精灵?我尝试使用CALater(核心动画层)制作动画,它就像一个魅力。我为'contentsRect'设置了动画。短代码示例(CALayer子类):

self.contents = [UIImage imageNamed:@"sprite-large.png"].CGImage;

NSMutableArray *frames = [NSMutableArray array]; // array of CGRect's
CAKeyframeAnimation *anim = [CAKeyframeAnimation animationWithKeyPath:@"contentsRect"];
anim.values = frames;
[self addAnimation:anim forKey:nil];

几周前我已经向GitHub发布了一些代码: https://github.com/Gerharbo/sprite-animation-layer/

它提供了一个示例精灵(不完美,但它有效)和一些示例代码。

玩得开心!

格哈德

答案 1 :(得分:0)

Gerharbo的方法基本上是一个纹理图集,但有一个更大的图像。我认为你会发现当你尝试使用很多图像时(这种方法很长),这种方法不能很好地扩展。您已经看到因内存不足而导致崩溃,因此完全避免使用UIImageView.animationImages是个好主意。这篇博客文章video-and-memory-usage-on-ios-devices描述了为什么你不能分配内存来保存主内存中的一堆图像,这正是你上面的代码试图做的事情。如果您环顾四周,您会发现许多其他开发人员ran into this same issue