setName with initWithContentsOfFile and imageNamed

I have a table view with an image cell… but the images are not within the main application bundle. If the images were in the bundle, then I could simply use this code to reference the image:

set theValue to NSImage's imageNamed_("green")

(above taken from Craig’s booklist example)

Since the images are not within the bundle, I’m using the following code:

set newImage to NSImage's alloc()'s initWithContentsOfFile_(posixPathOfImage)
newImage's setName_(theImageNameWithoutPathOrExtension)
set theValue to NSImage's imageNamed_(theImageNameWithoutPathOrExtension)

The above actually works… but only for a few seconds or minutes. So, I assume that this object is being released from memory. My question is: Once I use setName, how do I get it to be retained? It seemed like it would be more efficient to use the setName and access the image file just once… but maybe I’m wrong about that, and need to access the image file each time? Thanks in advance for any help!

You could try using initByReferencingFile instead. But I’m not sure why you want to refer by name – it seems to me that your theValue is just going to end up identical to newImage.

Shane -

Thank you for your reply - and your apt response to this question and my previous question. That is perfect! Using “initWithContentsOfFile” was causing my application to crash as the user scrolled in the tableView

That is what led me to try using “setName” and “imageNamed” (I thought by accessing the image once, it would be more efficient).

Thanks!