43
votes

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.

4
I know this is an old question, but the edit was new...the only way I know to manually clear the cache is through an undocumented, private method, so I suppose it's not really possible.Kevin Low
@KevinLow really? what method is that? I could be nice to play with that! private API rules do not apply for ad hoc only apps ;)Felipe Sabino
Haha, it's a class method on UIImage. [UIImage _flushSharedImageCache]; There's also [image removeFromCache]; as well as [UIImage removeImageNameFromCache:@"imageName"];Kevin Low
@Rajneesh071 good answer, but it appears that the cache that UIImage uses when the imageNamed: method is called is a memory cache, and not a file disk one (as it would make no sense, as the images from imageNamed: are already a file in the app bundle, differently from images loaded with imageWithData: and other similar methods). So the _flushSharedImageCache clears the allocated memory used by the images in cache, reducing the number of memory warnings in a heavy memory usage app (which was initially my intention by the time I asked this). But tks anyway ;)Felipe Sabino

4 Answers

7
votes

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.

3
votes

Answer taken from Kevin Low comment


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];
0
votes

The cache will be emptied when it needs to be, i.e. when the application receives a Low Memory Warning. There is no reason for you to do this yourself, and there is no public API available.

-1
votes

Maybe we have more then one methods for delete cached images, but i think you can used self.cache.removeAllObjects() which is one of the more methods if you use Cache() object.If i am wrong everybody can corrected me