views:

177

answers:

2

Hi,

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

+2  A: 

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

St3fan
There are also the convenience methods of `UIImageJPEGRepresentation` and UIImagePNGRepresentation` that can be used.
Marcus S. Zarra
A: 

In this answer, I provide code for an NSValueTransformer that lets you save UIImages into a transformable attribute in Core Data. You could do something similar for the drawn image in your context. If you need to preserve the vector elements, you could draw to a PDF context and save the data for that representation.

Brad Larson