views:

132

answers:

2

Hi,

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 ValueTransformer :


+ (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.

A: 

In transformedValue: you go straight from NSData to NSImage but in reverseTransformedValue: you go from NSImage to NSBitmapImageRep to NSData. So the two don't work together because they are using different methodologies. The data in your model is actually NSBitmapImageRep data and not NSImage data. The two need to be exact opposites of each other.

By the way, I posted a transformer for this recently in another post. You can see it here. And of course you don't need a custom transformer at all as you could just use NSKeyedArchiver. NSImage adheres to the NSCoder protocol so it would handle the transformation for you.

regulus6633
Thanks for your answer, I was writing my own answer at the same time so I missed yours. That would the trick though I really needed to store PNG representation because the iPhone SDK uses UIImage and not NSImage. The simple transformerless solution works well when staying on the mac but breaks when the sqlite database is read on the iPhone.
Gael
Yes, I used a custom transformer myself because I wanted to resize images to a small size before storing them in core data. I allow someone to drag an image to the image well, so a large image might be added and I didn't want to store such a large image... thus my transformer (although I removed the image resizing in the transformer I posted).
regulus6633
A: 

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 transformValue message is sent when the objects are saved (managed object -> store) and the reverseTransformedValue is 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 :

Important: Although the default transformer is the transformer specified by NSKeyedUnarchiveFromDataTransformerName, this transformer is actually used in reverse. If you specify the default transformer explicitly, Core Data would use it “in the wrong direction.”

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 :


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

+ (BOOL)allowsReverseTransformation
{
    return YES;   
}

- (id)reverseTransformedValue:(id)value
{
    if (value == nil) return nil;

    return [[[NSImage alloc] initWithData:value] autorelease];
}

- (id)transformedValue:(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;
}

Gael