Take the 2-minute tour ×

I am just beginning core data, and I wanted to know which attribute type I need to use so that I could store a UIImage and NSData in a CoreData 'file'. By this I mean what do I need to set the Attribute Type to.

share|improve this question

3 Answers 3

up vote 2 down vote accepted

The type is "Binary Data", which I believe ends up as an NSData type in the property on the entity.

However for a UIImage, you cannot just store that in a database as-is - you'll have to convert it to an NSData object first (convert to a JPG or PNG file) or store the UIImage to disk (as a PNG or JPG) and then store the file path in the database.

Unless it's a really small image you are better off not holding it in the database.

share|improve this answer

For the image, create a type Transformable, and then add the following to your .m file:

@implementation ImageToDataTransformer

+ (BOOL)allowsReverseTransformation {
    return YES;

+ (Class)transformedValueClass {
    return [NSData class];

- (id)transformedValue:(id)value {
    NSData *data = UIImagePNGRepresentation(value);
    return data;

- (id)reverseTransformedValue:(id)value {
    UIImage *uiImage = [[UIImage alloc] initWithData:value];
    return [uiImage autorelease];
share|improve this answer

UIImage can be stored as Transformable without writing any additional code, by setting the transformer to NSUnarchiveFromDataTransformerName.

See explanation at http://ootips.org/yonat/uiimage-in-coredata/ .

share|improve this answer

Your Answer


