过期的 [UIImage imageNamed:] 对象会怎样?
正如标题所说,假设我用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
只要您保留该图像,它就不会从缓存中清除。如果您在清除之前再次使用 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
我一直认为,如果您不保留
+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.它将为您加载新图像。这是文档:
UIImage imageNamed:
It will load the new image for you. Here is the documentation:
UIImage imageNamed:
据我了解,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.