开发者

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

开发者 https://www.devze.com 2022-12-17 18:39 出处:网络
my iPhone application should store some data persistantly by using Core 开发者_运维问答Data functionality.

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.


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消