[英]Core Data: How do I store a CGImageRef or custom data types?
my iPhone application should store some data persistantly by using Core Data functionality. 我的iPhone应用程序应使用“核心数据”功能持久存储一些数据。
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
警告:从不兼容的指针类型传递'setContext:'的参数1
How can you store a CGContextRef or in general a custom data type with Core Data? 如何存储CGContextRef或通常与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. 您首先需要将映像序列化为NSData(或CFData)实例。 Look at CGImageDestinationCreateWithData to do this.
查看CGImageDestinationCreateWithData以执行此操作。
In this answer , I provide code for an NSValueTransformer that lets you save UIImages into a transformable attribute in Core Data. 在此答案中 ,我提供了NSValueTransformer的代码,该代码使您可以将UIImages保存到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.
如果需要保留矢量元素,则可以绘制到PDF上下文并保存该表示形式的数据。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.