内存泄漏,iPad上有很多大图像 [英] Memory leak with a lot of big images in iPad
问题描述
我正在尝试将 UIImage 数据存储在 NSArray 中,实际上是60张图像,每张图像的大小为300kb。然后,我试图在 UIImageView 中为这些图像制作动画。
我的代码:
I'm trying to store UIImage datas in NSArray, actually 60 images, each with size of 300kb. Then, I'm trying to animate that images in UIImageView.
My code:
NSMutableArray *arr = [[NSMutableArray alloc] init];
for (int i=0; i<61; ++i) {
[arr addObject:[UIImage imageNamed:[NSString stringWithFormat:@"%i.png", i]]];
}
img.animationImages = arr;
img.animationDuration = 1.5;
img.contentMode = UIViewContentModeBottomLeft;
[img startAnimating];
当我在iPad模拟器4.3中测试时,它运行正常。< br>
当我想在我的设备(iPad 1)上测试它时,应用程序崩溃。
注意: App如果我评论此代码,则不会崩溃: [img startAnimating];
1。可能是什么问题呢?我认为这是记忆问题...?!
2.我可以在NSArray中存储一些UIImages吗?
When I test it in iPad Simulator 4.3, it works fine.
When I want to test it on my device (iPad 1), application crashes.
Note: App does not crashes if I comment this code: [img startAnimating];
1. What could be the problem? I think it is memory problem...?!
2. Can I store a loooots of UIImages in NSArray?
推荐答案
你的png文件可能有300KB,但这是一种压缩格式。
Your png files may have 300KB, but this is a compressing format.
要了解图像本身的大小你应该乘以每个像素的宽度,高度和字节数。
To get an idea of the size of the Image itself you should multiply the width, height and bytes per pixel.
ie如果图像的大小为1024 * 1024和RGBA型号,则图像本身在内存中的大小为4MB。这只是一张图片。如果你有300,它大概是120MB。
i.e. If an image has the size 1024 * 1024 and RGBA model, the images itself has an size of 4MB in the memory. And this is just one image. If you have 300 it is about 120MB.
注意:这只是一个粗略的经验法则,但它给你一个想法。
Note: this is only a rough rule of thumb, but it gives you an idea.
因此,您应该在数组中保留路径名并仅在需要时加载图像,并且应缩小缩略图并将其作为文件存储在磁盘上。不要只扩展UIImageView。
So you should keep path-names in the array and load the image only if need, and thumbnails should be resized and stored on the disk as files. Do not just scale the UIImageView.
编辑: 一篇很棒的文章,其中包含有关调整大小的代码。
a great article with code about resizing.
这篇关于内存泄漏,iPad上有很多大图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!