Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Avoid UIImage’s imageNamed - memory management

I was going through this link, where I came across a point Avoid UIImage’s imageNamed. For what reason we should avoid this?

like image 737
Nitish Avatar asked Aug 26 '11 06:08

Nitish


4 Answers

It caches the images and not releasing it, until it receives memory warning. I am not sure, but i guess it might prone to app crashes, if your app has a lot of images (big ones)

For me, I normally use "imageWithContentsOfFile":

[UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:fileName ofType:nil]]
like image 90
kjiyoung Avatar answered Nov 12 '22 16:11

kjiyoung


In Apple WWDC videos section they are saying imageNamed leaked once..But no longer do..I remember seeing the video, but cant remember which video section..It was in 2010 WWDC I think..ImageNamed still caches images..From documentation of imageNamed..

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.

But I think these cache's will be cleared when any low memory condition occurs..Just for your information..I am an avid fan of imageNamed and I always use it..I never had any memory issues with it..

like image 33
Krishnabhadra Avatar answered Nov 12 '22 17:11

Krishnabhadra


You should read the article to the end.

Avoid UIImage’s imageNamed: Alex Curylo has written an absolutely great article about the problems with UIImage’s imageNamed: static method. It seems (and in my tests this appears to be true) that the iPhone OS (versions 2.0 and 2.1 at least) uses an internal cache for images loaded from disk using imageNamed:, and that in cases of low memory this cache is not cleared up completely (this seems to be corrected with version 2.2, though, but I cannot confirm).

like image 42
Pierre Avatar answered Nov 12 '22 17:11

Pierre


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 this question

like image 40
Fran Sevillano Avatar answered Nov 12 '22 15:11

Fran Sevillano