您的位置:首页 > 移动开发 > IOS开发

IOS 内存占用问题之 ImageIO_PNG_Data

2015-06-18 15:35 756 查看

iOS 内存占用问题之ImageIO_PNG_Data

问题来源

做APP调试的时候,发现总是收到 didReceiveMemoryWarning,打开 Instruments 的 Memory Leaks 一看,ImageIO_PNG_Data占了好大的内存(近100MB)。

其实这个问题零零碎碎地在很多地方都有人提到过,但是提到ImageIO_PNG_Data似乎不多,碰到问题的时候不好搜索,这里着重提一下。

问题分析

这篇文章里提到了关于图片缓存的问题,看起来和我们碰到的这个问题有关。

具体来说,就是我们通常会用两种方式加载图片:

UIImage *img = [UIImage imageNamed:@"myImage"]; // caching
// or
UIImage *img = [UIImage imageWithContentsOfFile:@"myImage"]; // no caching
根据苹果官方参考:

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.

imageNamed: 会将图片加载到系统缓存,适合需要重复利用的图片,提高加载速度

imageWithContentsOfFile: 不会将图片加载到系统缓存,适合不重复利用的图片

问题解决

检查使用到 imageNamed: 的地方,根据需求场景,将不必要缓存的部分全部替换为 imageWithContentsOfFile:,会发现ImageIO_PNG_Data的内存占用瞬间就降下来了。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: