过期的 [UIImage imageNamed:] 对象会怎样?

发布于 2024-09-11 09:38:32 字数 212 浏览 4 评论 0原文

正如标题所说,假设我用 imageNamed: 实例化一个 UIImage 对象。这会创建并返回一个指向自动缓存 UIImage 对象的指针,对吗?

如果框架决定需要清除其缓存(无论出于何种原因,尽管很可能内存不足),会发生什么?我是否会陷入死指针的困境?

编辑:我问的原因不是我打算使用 imageName,而是在我自己的资源管理类中复制其核心功能。

As the title says, suppose I instantiate an UIImage object with imageNamed:.. this creates and returns a pointer to an autocached UIImage object, correct?

What happens if the frameworks decides that its cache needs to be purged (for whatever reason, though most likely running low on memory) ? Do I just get stuck with a dead pointer?

edit: The reason I ask, is not that I plan to use imageName, but rather am duplicating its core functionality in my own resource management class.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

傾旎 2024-09-18 09:38:32

只要您保留该图像,它就不会从缓存中清除。如果您在清除之前再次使用 imageNamed 并且不再有指向图像的活动指针,则缓存会有所帮助。在这种情况下,您只需再次取回图像,而不需要重新加载它。 Apple 最终通过 NSCache

As long as you retain the image, it won't be purged from the cache. The caching helps if you use imageNamed again sometime before a purge and don't have anymore active pointers to the image. In this case you'll just get the image back again, without the need to have it reloaded. Apple finally exposed this functionality for us to use in our own apps with NSCache

岁吢 2024-09-18 09:38:32

我一直认为,如果您不保留 +imageNamed: 给您的对象,则它不一定在初始调用后的任何时间都有效。不过,保留它似乎可以让该实例保持活力。

I've always assumed that, if you don't retain the object +imageNamed: gives you, it's not necessarily going to be valid at any time after that initial call. Retaining it, though, seems to keep that instance alive.

泪眸﹌ 2024-09-18 09:38:32

它将为您加载新图像。这是文档:

该方法在系统缓存中查找
对于具有指定的图像对象
名称并返回该对象,如果
存在。如果匹配的图像对象是
尚未在缓存中,此方法
从加载图像数据
指定文件,缓存它,然后
返回结果对象。

UIImage imageNamed:

It will load the new image for you. Here is the documentation:

This method looks in the system caches
for an image object with the specified
name and returns that object if it
exists. If a matching image object is
not already in the cache, this method
loads the image data from the
specified file, caches it, and then
returns the resulting object.

UIImage imageNamed:

梨涡少年 2024-09-18 09:38:32

据我了解,imageNamed 的“自动缓存”问题是,一旦您使用 UIImage::imageNamed 打开图像,它就会被系统缓存并且永远不会释放。这就是为什么 Apple 鼓励使用 UIImage::imageWithContentsOfFile 而不是偶尔使用的图像。对于 UI 中经常使用的图像,imageNamed 就可以了。

当您保留实例时,它不会被卸载。

As I understand it the "autocache" issue with imageNamed is that once you open an image with UIImage::imageNamed, it's cached by the system and never released. That's why Apple encourages usage of UIImage::imageWithContentsOfFile instead for images that are used occasionally. For images that are used very often in the UI, imageNamed is OK.

Your instance, while you retain it, will not be unloaded.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文