Avoid UIImages imageNamed - memory management

I walked through this , where I came across a point Avoid UIImages imageNamed . Why should we avoid this?

Thanks,
Nitish

+6
source share
4 answers

Apple’s WWDC video section says that imageNamed leaked once ... But it doesn’t. I remember seeing the video, but I can’t remember which section of the video. It was in 2010 WWDC, I think ... ImageNamed is still caching images .. With imageNamed documentation ..

This method scans the system caches for the image object using the specified name and returns this object if it exists. If the match 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.

But I think this cache will be cleared when some low memory condition occurs. Just for your information. I am an avid imageNamed fan and I always use it. I have never had a memory problem.

+6
source

It caches images and does not release them until it receives a warning about saving memory. I'm not sure, but I suppose this could lead to application crashes if your application has a lot of images (large)

For me, I usually use "imageWithContentsOfFile":

[UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:fileName ofType:nil]] 
+7
source

You must read the article to the end.

Avoid UIImages imageNamed: Alex Kurilo wrote an excellent article about problems with UIImages imageNamed: static method. It seems (and in my tests it is true) that iPhone OS (at least versions 2.0 and 2.1) uses an internal cache for images downloaded from disk using imageNamed :, and in the case of low memory, this cache is not cleared (this, most likely fixed with version 2.2, but I can’t confirm).

+2
source

It says that it should be avoided in versions 2.0 and 2.1 of the SDK. It has been working properly for a long time.

Take a look at the question

+1
source

Source: https://habr.com/ru/post/895943/


All Articles