Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
358 views
in Technique[技术] by (71.8m points)

objective c - Is there a way to clear the cache used by UIImage class?

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

From apple documentation: https://developer.apple.com/documentation/uikit/uiimage/1624146-imagenamed

imageNamed:

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.

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)

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)

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?

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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

There is no way I know of to manually clear this iOS managed cache. In general, this is a red herring. When the os manages something for you, you don't need to worry about it. As long as you are correctly releasing anything you alloc/retain and handling memory warnings appropriately you're doing your part.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...