You can store a UIImage (or an NSImage on Mac) as raw data. This can be useful if you’d like to save it in Core Data.
Here’s how you convert a UIImage into NSData:
1 |
NSData *imageData = UIImagePNGRepresentation(yourImage); |
To convert NSData back to a UIImage, you can do this:
1 |
UIImage *image = [[UIImage alloc]initWithData:yourData]; |
The UIImage class is not available on Mac OS X, but its counterpart NSImage is almost identical.
– http://stackoverflow.com/questions/6476929/convert-uiimage-to-nsdata
NSValueTransformer
Core Data can convert images to and from data on the fly by using an NSValueTransformer.
Here’s one that works for images:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
@implementation PatchBayTransformer +(BOOL)allowsReverseTransformation { return YES; } +(Class)transformedValueClass { return [NSData class]; } - (id)transformedValue:(id)value { // takes the UIImage into data and returns it NSData *imageData = UIImagePNGRepresentation(value); return imageData; } - (id)reverseTransformedValue:(id)value { // takes data and returns it as a UIImage UIImage *image = [[UIImage alloc]initWithData:value]; return image; } |