有没有办法清除 UIImage 类使用的缓存? [英] Is there a way to clear the cache used by UIImage class?

查看:38
本文介绍了有没有办法清除 UIImage 类使用的缓存?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

众所周知,UIImage 在使用 imageNamed: 方法加载图像时缓存其图像数据.

It is well known that UIImage caches its image data when the image is loaded using the imageNamed: method.

来自苹果文档:https://developer.apple.com/documentation/uikit/uiimage/1624146-imagenamed

图片名称:

讨论:这个方法在系统缓存图像对象指定的名称并返回对象(如果存在).如果一个匹配图像对象不在缓存,此方法加载图像来自指定文件的数据,缓存it,然后返回结果对象.

Discussion: 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.

因此,在使用 imageNamed: 加载多个图像后,我注意到内存使用量大幅增加,而且即使在加载图像的控制器被解除分配后,内存仍在使用中.(至少当我分配同一个控制器时它没有再次增加)

Because of that, after loading several images with imageNamed: I noticed a large increase of memory usage and also that the memory was kept in use even after the controller that loaded the images was dealloc. (at least it didn't increase again when I alloc the same controller)

这让我想知道是否有任何方法可以在我的应用程序生命周期的任何给定时间以编程方式清除 UIImage 使用的缓存,甚至控制一些缓存参数(例如它可以使用的最大内存)

That made me wonder if there is any way to clear the cache used by UIImage programmatically at any given time of my application lifecycle or even control some cache parameters (like the maximum memory that it can use, for example)

我知道我可以通过使用 initWithDataimageWithDataimageWithContentsOfFile 或任何其他初始化程序而不是 imageNamed 来轻松解决这个问题,但是在使用多个图像时需要这种缓存行为,例如在 UITableView 中.

I know that I could easily solve this problem by using initWithData, imageWithData, imageWithContentsOfFile or any other initializer instead of imageNamed, but this cache behavior is desired when using several images, like inside a UITableView.

对如何实现这一目标有任何想法吗?

Any thoughts on how to accomplish that?

在得到一些答案之后,我只想说清楚,需要做某事和有可能做某事之间存在巨大差距.正如我所指出的,我知道操作系统会为我处理缓存,我只是想看看 iOS SDK 施加的限制.

After some answers I just want to make it clear that there is a huge gap between needing to do something and having the possibility to do something. As I pointed out, I know that the OS takes care of that cache for me, I am just trying to see the limitations that the iOS SDK imposes.

推荐答案

答案取自 Kevin Low 评论

如果您可以使用私有 API(例如在仅 AdHoc 的应用程序中),请使用此 UIImage 方法从缓存中删除所有图像

If you can use private APIs (as in an AdHoc only app, for example) use this UIImage method to remove all images from cache

[UIImage _flushSharedImageCache];

这篇关于有没有办法清除 UIImage 类使用的缓存?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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