2

I am storing OSX icons (NSImage) in core data as Transformable attributes. IOS is unable to decode the NSImage it seems.

What are my options for storing a compatible image in Core Data, using a transformable (or other) attribute and accessing the image on iOS.

Currently the user can drop their own image onto the NSImageWell and this is stored in the objective-c class as an NSImage. This obviously gets archived into the core data transformable and when iOS tried to retrieve it it throws an exception because it does not understand an NSImage object.

Is there something other than NSImage I can use on OS X that will be compatible with iOS ?

Duncan Groenewald
  • 8,496
  • 6
  • 41
  • 76

1 Answers1

5

PNG (= Portable Network Graphics) is an option.

You can convert an NSImage to PNG data with (copied from How to save a NSImage as a new file):

NSImage *image = ...;
NSBitmapImageRep *imgRep = [[image representations] objectAtIndex: 0];
NSData *pngData = [imgRep representationUsingType: NSPNGFileType properties: nil];

and create an UIImage from PNG data with

UIImage *image = [UIImage imageWithData:pngData];
Community
  • 1
  • 1
Martin R
  • 529,903
  • 94
  • 1,240
  • 1,382
  • +1. Unless you're keeping image data purely for private data within an instance of an application, it should always be encoded to a standard format, and PNG is a fast, easy, efficient choice generally. (It would be very rare to justify explicitly keeping image data in ANY nonstandard encoding, although there are sometimes reasons.) – Ben Zotto Aug 18 '13 at 19:17