Core Data: How to store a CGImageRef or custom data types?


my iPhone application should store some data persistantly by using Core Data functionality.

Here is a code snippet:

NSManagedObject *entity = [NSEntityDescription insertNewObjectForEntityForName:@"anEntity" inManagedObjectContext:self.managedObjectContext];

// ...
CGContextRef context;
// ...
[entity setContext:context];
NSError *error;
[self.managedObjectContext save:&error];
// ...

My data model defines context's type as "transformable". Is this right? At build time I get the following warning:

warning: passing argument 1 of 'setContext:' from incompatible pointer type

How can you store a CGContextRef or in general a custom data type with Core Data? Does this require the implementation of a Encoder/Decoder?

Thank you, Norbert

You will need to serialize the image to an NSData (or CFData) instance first. Look at CGImageDestinationCreateWithData to do this.