UIImageView Animation too much memory consumption(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 内存消耗过多
基础教程推荐
- Kivy Buildozer 无法构建 apk,命令失败:./distribute.sh -m “kivy"d 2022-01-01
- 如何在 UIImageView 中异步加载图像? 2022-01-01
- android 应用程序已发布,但在 google play 中找不到 2022-01-01
- 如何让对象对 Cocos2D 中的触摸做出反应? 2022-01-01
- 如何在没有IB的情况下将2个按钮添加到右侧的UINavigationbar? 2022-01-01
- 当从同一个组件调用时,两个 IBAction 触发的顺序是什么? 2022-01-01
- 如何在 iPhone 上显示来自 API 的 HTML 文本? 2022-01-01
- 在 gmail 中为 ios 应用程序检索朋友的朋友 2022-01-01
- Android:对话框关闭而不调用关闭 2022-01-01
- UIWebView 委托方法 shouldStartLoadWithRequest:在 WKWebView 中等效? 2022-01-01