NSImage to NSData, then to UIImage
OS X:
Instead of using NSKeyedArchiver
to convert an NSImage
to NSData
, use NSImage
's TIFFRepresentation
method:
NSData *imageData = [self.someImage TIFFRepresentation];
// save imageData to file
iOS:
Read the image data from the file, then convert it to a UIImage using UIImage's +imageWithData:
convenience constructor:
NSData *imageData = ...; // load imageData from file
UIImage *image = [UIImage imageWithData: imageData];
You need to create a NSBitmapImageRep
and then save that, then read the NSData
to UIImage
with +[UIImage imageWithData:]
:
First in OS X, save the data:
NSString *filepath;
NSBitmapImageRep *imageRep = [NSBitmapImageRep imageRepWithContentsOfFile:filepath];
NSData *data = [imageRep representationUsingType:NSPNGFileType properties:nil];
// Save the data
You could also use imageRepWithData:
if you have NSData
of the image already - the above will load it from a file (like you can also do with NSImage
).
Then in iOS:
NSData *data; // Load from a file
UIImage *image = [UIImage imageWithData:data];
See here for the other allowed keys for the dictionary in representationUsingType:properties:
.