tags:

views:

85

answers:

4

As the title says, suppose I instantiate an UIImage object with imageNamed:.. this creates and returns a pointer to an autocached UIImage object, correct?

What happens if the frameworks decides that its cache needs to be purged (for whatever reason, though most likely running low on memory) ? Do I just get stuck with a dead pointer?

edit: The reason I ask, is not that I plan to use imageName, but rather am duplicating its core functionality in my own resource management class.

+1  A: 

I've always assumed that, if you don't retain the object +imageNamed: gives you, it's not necessarily going to be valid at any time after that initial call. Retaining it, though, seems to keep that instance alive.

Noah Witherspoon
uhm, I think he asks about the cache when he calls [UIImage imageNamed:] not about he use the UIImage object again
vodkhang
@vodkhang - yes, technically that is true, although if by retaining the object, i am able to influence some control over the cache, then that was very helpful for me to know.
bitcruncher
+1  A: 

It will load the new image for you. Here is the documentation:

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.

UIImage imageNamed:

vodkhang
+1  A: 

As I understand it the "autocache" issue with imageNamed is that once you open an image with UIImage::imageNamed, it's cached by the system and never released. That's why Apple encourages usage of UIImage::imageWithContentsOfFile instead for images that are used occasionally. For images that are used very often in the UI, imageNamed is OK.

Your instance, while you retain it, will not be unloaded.

jpmartineau
It's not "never" released, the cache may be cleared any time. But the UIImage you get back, if retained, is kept... and if you are asking for the same image in a number of places they will all use the same cached data as a base.
Kendall Helmstetter Gelner
It _may_ be cleared, but apparently, sometimes it isn't: http://www.alexcurylo.com/blog/2009/01/13/imagenamed-is-evil/It seems this was a bug on iOS < 3.0
jpmartineau
+4  A: 

As long as you retain the image, it won't be purged from the cache. The caching helps if you use imageNamed again sometime before a purge and don't have anymore active pointers to the image. In this case you'll just get the image back again, without the need to have it reloaded. Apple finally exposed this functionality for us to use in our own apps with NSCache

Joshua Weinberg
retain seems to be the way to go. and thanks for pointing me to NSCache, though it does not apply directly here.
bitcruncher
Although not in iPhone applications (yet).
Kendall Helmstetter Gelner
its in iOS 4.0, see http://developer.apple.com/iphone/library/documentation/Cocoa/Reference/NSCache_Class/Reference/Reference.html
Joshua Weinberg