I am trying to store a PNG image in a core data store backed by an sqlite database. Since I intend to use this database on an iPhone I can’t store NSImage objects directly. I wanted to use bindings and an NSValueTransformer subclass to handle the transcoding from the NSImage (obtained by an Image well on my GUI) to an NSData containing the PNG binary representation of the image. I wrote the following code for the NSValueTransformer :
+ (Class)transformedValueClass
{
return [NSImage class];
}
+ (BOOL)allowsReverseTransformation
{
return YES;
}
- (id)transformedValue:(id)value
{
if (value == nil) return nil;
return [[[NSImage alloc] initWithData:value] autorelease];
}
- (id)reverseTransformedValue:(id)value
{
if (value == nil) return nil;
if(![value isKindOfClass:[NSImage class]])
{
NSLog(@"Type mismatch. Expecting NSImage");
}
NSBitmapImageRep *bits = [[value representations] objectAtIndex: 0];
NSData *data = [bits representationUsingType:NSPNGFileType properties:nil];
return data;
}
The model has a transformable property configured with this NSValueTransformer. In Interface Builder a table column and an image well are both bound to this property and both have the proper value transformer name (an image dropped in the image well shows up in the table column). The transformer is registered and called every time an image is added or a row is reloaded (checked with NSLog() calls). The problem arises when I am trying to save the managed objects. The console output shows the error message:
[NSImage length]: unrecognized selector sent to instance 0x1004933a0
It seems like core data is using the value transformer to obtain the NSImage back from the NSData and then tries to save the NSImage instead of the NSData.
There are probably workarounds such as the one presented in this post but I would really like to understand why my approach is flawn.
Thanks in advance for your ideas and explanations.
I eventually got it to work (best to sleep on it). There were two problems. The first one was that I specified the same value transformer both in IB bindings and in the core data model and it was redundant.
My understanding is that IB bindings specified value transformers actually transform values between the views and the controllers whereas the core data value transformer acts as a middleman between the managed objects and the persistent store.
To make the matter worse, the second problem was that my value transformer was upside down. The
transformValuemessage is sent when the objects are saved (managed object -> store) and thereverseTransformedValueis sent when loading the values from the store which is somewhat the opposite direction of the transformers used when binding view objects to model objects. I got the hint from the core data programming guide where you can read the following somewhat cryptic note :Even though my program is working so far, I would like to have confirmation from someone with core data experience and I would appreciate if you could point me to some documentation on that matter that I might have missed.
Thanks in advance.
The working code :