iOS4 调用 ImageNamed:仍然泄漏或导致内存问题?
显然,由于内存不足问题,我的应用程序在第二代 iPod 上崩溃了。我所做的是当用户滚动时在scrollView + pageControl 中的每个视图上调用图像。当收到内存警告后,应用程序到达特定点时崩溃。当我收到警告时,我试图释放视图,但它仍然导致崩溃。
我在 google 上搜索了 ImageNamed: ,显然这个 api 调用中存在问题,但大多数文章都说它在最近的 iOS 版本中已修复。
我通过调用图像 imageWithContentOfFile 而不是 imageNamed 解决了这个问题,但我想知道 ImageNamed 是否仍然会导致内存泄漏或在释放视图时不会释放内存。
So apparently, my app crashes on ipod 2nd generation due to low memory issue. What I do was calling image on each view within scrollView + pageControl when user scrolls. And app crashed when it reached a particular point after got memory warning. I tried to free up view when I got warning but it still caused crash.
I googled about ImageNamed: and apparently there was issue within this api call, but most article said it was fixed in recent iOS version.
I fixed this problem with calling image imageWithContentOfFile instead imageNamed, but I'm wondering if ImageNamed still causes memory leak or not free up when it view is released.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
imageNamed:
不会导致泄漏,但它经常被误解,这就是在使用它时导致内存问题的原因。它在加载后缓存未压缩的图像,这意味着内存中立即有该图像的 2 个副本。如果您将其用于小型的、经常使用的图像(例如图标),那么这非常好,因为运行时不必从磁盘中获取文件 - 它已经在缓存中可用。当用户使用imageNamed:
加载大图像(例如用相机拍摄的 4MP 图像)时,这会给用户带来麻烦。该图像占用相当多的内存:400万像素,每个像素输入4个字节= 16MB 内存,两倍。如果您使用该方法为幻灯片、照片共享、相机应用程序或其他任何内容加载图像,那么它的加载速度会非常快。因此,如果这些功能不符合您的需要,请使用其他 UIImage 加载方法之一。用户会感谢你的。
注意:此信息来自介绍 UIKit 渲染会话的 Apple 工程师(我认为是 #121)。希望我的笔记是正确的:)
imageNamed:
doesn't cause a leak, but it is frequently misunderstood which is what leads to memory issues when it's used. It caches the uncompressed image after it is loaded, which means there are immediately 2 copies of that image in memory. If you use it for small, frequently used images (such as icons), this is great because the runtime doesn't have to fetch the file off disk - it's already available in the cache. Where this gets users into trouble is when they useimageNamed:
to load a large image, say a 4MP image taken with a camera. That image takes up quite a bit of memory: 4 million pixels, types 4 bytes per pixel = 16MB of memory, TWICE. If you use that method to load images for your slideshow, photo sharing, camera app, or whatever, it adds up real fast.So if those features don't fit what you need, use one of the other UIImage loading methods. You users will thank you.
Note: This information comes from the Apple Engineer which presented the UIKit rendering session (#121 I think it was). Hopefully my notes are correct :)