动画的UIImageView过多的内存消耗 [英] UIImageView Animation too much memory consumption

查看:136
本文介绍了动画的UIImageView过多的内存消耗的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有记忆和图像的问题,我动画。第一件事首先,我使用ARC。在我最初的屏幕,我对60张图片的动画。我用这code动画:

I have a problem with memory and images that I am animating. First thing first, I am using ARC. On my initial screen I have about 60 images to animate. I am using this code for animation:

NSMutableArray *images = [[NSMutableArray alloc] init];
int animationImageCount = 61;
for (int i = 1; i < animationImageCount; i++) {
    [images addObject:[UIImage imageNamed:[NSString stringWithFormat:@"s-%d", i]]];
}
self.fullAnimatedImage = [UIImage imageNamed:[NSString stringWithFormat:@"s-%d", animationImageCount - 1]];

self.animatedImageView.image = self.fullAnimatedImage;
self.animatedImageView.animationImages = [NSArray arrayWithArray:images];
self.animatedImageView.animationDuration = images.count/30;
self.animatedImageView.animationRepeatCount = 1;
[self.animatedImageView startAnimating];

// since there is no official callback method when UIImageView animation completes,
// we can just call a method after animation time has passed
[self performSelector:@selector(animationFinished) withObject:nil
           afterDelay:self.animatedImageView.animationDuration];

问题是,内存消耗大约为300 MB,当然它崩溃的iPhone 4。当我注释掉code的那部分,内存消耗约为40 MB,这是确定。我试过图像属性设置为无并没有什么。我试图把它放在 @autoreleasepool {} ,但它不释放它的内容。你有什么想法如何优化呢?

The problem is that memory consumption is about 300 MB and of course it crashes on iPhone 4. When I comment out that part of code, memory consumption is about 40 MB, that's ok. I've tried setting image property to nil and nothing. I've tried to put it in @autoreleasepool {} but it does not release it's content. Do you have any idea how to optimise this?

推荐答案

您应该尝试更换 [UIImage的imageNamed:] [UIImage的imageWithContentsOfFile: ]

从文档:

如果您有将只显​​示一次图像文件,并希望确保它不会添加到系统的缓存,您应该使用imageWithContentsOfFile创建你的形象:。这将让你的单次使用的图像从系统图像缓存,可能会提高你的应用程序的内存使用特性。

If you have an image file that will only be displayed once and wish to ensure that it does not get added to the system’s cache, you should instead create your image using imageWithContentsOfFile:. This will keep your single-use image out of the system image cache, potentially improving the memory use characteristics of your app.

这篇关于动画的UIImageView过多的内存消耗的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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