作者热门文章
- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
我在制作动画时遇到内存和图像问题。首先,我正在使用 ARC。在我的初始屏幕上,我有大约 60 张要制作动画的图像。我将这段代码用于动画:
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,没关系。我试过将 image 属性设置为 nil 什么也没有。我试图将它放在 @autoreleasepool {}
中,但它不会释放它的内容。您知道如何优化它吗?
最佳答案
您应该尝试将 [UIImage imageNamed:]
替换为 [UIImage 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.
关于ios - UIImageView动画太耗内存,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35180145/
我是一名优秀的程序员,十分优秀!