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

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

问题描述

显然,由于内存不足问题,我的应用程序在第 2 代 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.

我在谷歌上搜索了 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.

我通过调用 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 内存,两倍.如果您使用该方法为幻灯片、照片共享、相机应用程序或其他任何内容加载图像,那么它的添加速度会非常快.

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天全站免登陆