views:

1218

answers:

3

What is the correct way to store an NSImage in a Core Data Model? I have assumed adding an Attribute to an Entity and giving it the Type "Binary" should work, but it is not working. I have a table with a column of NSImageCells and it is not showing anything.

+7  A: 

You'll need to create an NSData representation of it.

Have a look at Non-Standard Persistent Attributes. Especially the section under Transformable Attributes, and Custom Code if that doesn't sort you out.

Matthew Schinckel
+5  A: 

This doesn't answer the exact question you asked, but depending on how many images you are storing it can be more efficient to store only paths or URLs to the images, saved in your own location, and load them as required.

sbooth
+8  A: 

If you can work in 10.5+, the easiest way is to store the NSImageReps for the image in "Transformable" attribute. By default, the transformable attributes use NSCoding to archive their values on set and unarchive on access. This saves you from having to write custom getters/setters. If you want to get fancy, you could write a custom NSValueTransformer that converts an image to an acrhived version of its representations and visa versa on get. If you're using 10.4, then you have to write custom getters/setters (see Apple's docs on creating Non-standard persistent attributes. You can get the image's image reps by sending the NSImage a -representations message.

If you want to display the images in a UI via bindings, you should also read the Displaying Images Using Bindings section of the Cocoa Bindings Programming Topics.

Barry Wark