views:

1425

answers:

5

I have a Core Data app with a fairly simple data model. I want to be able to store instances of NSImage in the persistent store as PNG Bitmap NSData objects, to save space.

To this end, I wrote a simple NSValueTransformer to convert an NSImage to NSData in PNG bitmap format. I am registering the value transformer with this code in my App delegate:

+ (void)initialize
{
    [NSValueTransformer setValueTransformer:[[PNGDataValueTransformer alloc] init] forName:@"PNGDataValueTransformer"];
}

In my data model, I have set the image attribute to be Transformable, and specified PNGDataValueTransformer as the value transformer name.

However, my custom value transformer is not being used. I know this as I have placed log messages in my value transformer's -transformedValue: and -reverseTransformedValue methods which are not being logged, and the data that is being saved to disk is just an archived NSImage, not the PNG NSData object that it should be.

Why is this not working?

Here is the code of my value transformer:

@implementation PNGDataValueTransformer

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

+ (BOOL)allowsReverseTransformation
{
    return YES;
}

- (id)transformedValue:(id)value
{
    if (value == nil) return nil;
    if(NSIsControllerMarker(value))
     return value;
    //check if the value is NSData
    if(![value isKindOfClass:[NSData class]])
    {
     [NSException raise:NSInternalInconsistencyException format:@"Value (%@) is not an NSData instance", [value class]];
    }
    return [[[NSImage alloc] initWithData:value] autorelease];
}

- (id)reverseTransformedValue:(id)value;
{
    if (value == nil) return nil;
    if(NSIsControllerMarker(value))
     return value;
    //check if the value is an NSImage
    if(![value isKindOfClass:[NSImage class]])
    {
     [NSException raise:NSInternalInconsistencyException format:@"Value (%@) is not an NSImage instance", [value class]];
    }
    // convert the NSImage into a raster representation.
    NSBitmapImageRep* bitmap    = [NSBitmapImageRep imageRepWithData: [(NSImage*) value TIFFRepresentation]];
    // convert the bitmap raster representation into a PNG data stream
    NSDictionary* pngProperties = [NSDictionary dictionaryWithObject:[NSNumber numberWithBool:NO] forKey:NSImageInterlaced];
    // return the png encoded data
    NSData* pngData             = [bitmap representationUsingType:NSPNGFileType properties:pngProperties];
    return pngData;
}

@end
A: 

If nothing in your code elsewhere explicity uses the PNGDataValueTransformer class, then the +initialize method for that class will never be called. Specifying the name in your Core Data model will not trigger it either - it will simply try looking up a value transformer for that name, which will return nil, since no transformer instance has yet been registered under that name.

If this is indeed what's happening in your case, simply add a call to [PNGDataValueTransformer initialize] somewhere in your code before your data model gets accessed, e.g. in the +initialize method of whatever class it is that's using this data model. That should trigger the value transformer instance being created and registered so that Core Data can access it when it needs to.

Brian Webster
Hi Brian, the `+initialize` method is in my app delegate, not the value transformer and I can definitely verify that it's being called. If I put a breakpoint in the `-init` method of my NSValueTransformer, it is definitely initialized.If I set the attribute to be of type "binary" and apply my value transformer to each binding in IB, it works fine. I just don't understand why the custom transformer is not being used when I specify the attribute type as Transformable.
Rob Keniger
A: 

If I'm not mistaken, your value transformer has a reversed direction. I do the same thing in my application, using code like the following:

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

+ (BOOL)allowsReverseTransformation 
{
 return YES; 
}

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

 // I pass in raw data when generating the image, save that directly to the database
 if ([value isKindOfClass:[NSData class]])
  return value;

 return UIImagePNGRepresentation((UIImage *)value);
}

- (id)reverseTransformedValue:(id)value
{
 return [UIImage imageWithData:(NSData *)value];
}

While this is for the iPhone, you should be able to swap in your NSImage code at the appropriate locations. I simply haven't tested my Mac implementation yet.

All I did to enable this was to set my image property to be transformable within the data model, and specify the name of the transformer. I didn't need to manually register the value transformer, as you do in your +initialize method.

Brad Larson
Thanks Brad. I tried this and it didn't make any difference, my value transformer is just ignored.
Rob Keniger
+1  A: 

It seems registering the transformer has no effect on wether Core Data will use it or not. I've been playing with the PhotoLocations sample code and removing the transformer registration has no effect. As long as your ValueTransformerName is the name of your class, and that your transformer's implementation is included with the target, it should work.

If there is no code execution in the transformer, then the code in the transformer is irrelevant. The problem must be somewhere else in the core data stack, or in the declaration of the transformer.

CornPuff
A: 

Check out the example code here.

Does this do what you want?

@implementation UIImageToDataTransformer


+ (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];
}
RyanJM
A: 

It turns out that this is actually a bug in the frameworks. See this post by an Apple employee on the Cocoa-Dev mailing list:

http://lists.apple.com/archives/Cocoa-dev/2009/Dec/msg00979.html

Rob Keniger