iOS4调用ImageNamed:仍然泄漏或导致内存问题? [英] iOS4 calling ImageNamed: still leak or cause memory issue?

查看:134
本文介绍了iOS4调用ImageNamed:仍然泄漏或导致内存问题?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

显然,由于内存不足问题,我的应用程序在ipod第二代崩溃了。我做的是在用户滚动时在scrollView + pageControl中的每个视图上调用图像。当内存警告到达特定点后,应用程序崩溃了。当我收到警告时,我试图释放视图,但它仍然导致崩溃。

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.

我用Google搜索了关于ImageNamed:显然这个api调用中存在问题,但大多数文章说它是在最近的iOS版本中修复的。

I googled about ImageNamed: and apparently there was issue within this api call, but most article said it was fixed in recent iOS version.

我通过调用image imageWithContentOfFile而不是imageNamed解决了这个问题,但是我想知道ImageNamed是否仍会导致内存泄漏或者当没有释放时它的观点已经发布。

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.

推荐答案

imageNamed:不会导致泄漏,但它是经常被误解,这是导致内存问题的原因。它在加载后缓存未压缩的图像,这意味着该图像在内存中立即有2个副本。如果将它用于经常使用的小图像(例如图标),这很好,因为运行时不必从磁盘上获取文件 - 它已经在缓存中可用。这会让用户遇到麻烦的是当他们使用 imageNamed:来加载大图像时,比如使用相机拍摄的4MP图像。该图像占用了相当多的内存:400万像素,每像素4个字节= 16MB的内存,TWICE。如果您使用该方法为幻灯片,照片共享,相机应用程序或其他任何内容加载图像,它会加快速度。

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 use imageNamed: 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.

因此,如果这些功能不符合您的需要,请使用其他一种UIImage加载方法。您的用户会感谢您。

So if those features don't fit what you need, use one of the other UIImage loading methods. You users will thank you.

注意:此信息来自提供UIKit渲染会话的Apple工程师(#121我认为它是)。希望我的笔记是正确的:)

Note: This information comes from the Apple Engineer which presented the UIKit rendering session (#121 I think it was). Hopefully my notes are correct :)

这篇关于iOS4调用ImageNamed:仍然泄漏或导致内存问题?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆