UIImageView Animation 内存消耗过多

UIImageView Animation too much memory consumption(UIImageView Animation 内存消耗过多)

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

问题描述

我正在制作动画的内存和图像有问题.首先,我使用的是 ARC.在我的初始屏幕上,我有大约 60 张要制作动画的图像.我正在将此代码用于动画:

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 上崩溃了.当我注释掉那部分代码时,内存消耗大约是 40 MB,没关系.我已经尝试将图像属性设置为 nil 并且什么都没有.我试图把它放在 @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:].

You should try replacing [UIImage imageNamed:] with [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 Animation 内存消耗过多的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本文标题为:UIImageView Animation 内存消耗过多

基础教程推荐