简体   繁体   English

核心数据:如何存储CGImageRef或自定义数据类型?

[英]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.

相关问题 如何在核心数据中存储自定义配置类(即键值设计或自定义类) - how to store a custom config class in core data (i.e. key-value design or custom class) 如何在未启用iCloud时存储核心数据日志? - How do I store core data logs when iCloud isn't enabled? 在 SwiftUI 中,如何使用核心数据将触摸 ID 存储为人员/类的属性并记录它们? - In SwiftUI, How do I use core data to store touch ID as an attribute of a person/class and record them? 如何加密整个核心数据存储,而不是单个记录? - How do I encrypt the entire Core Data store, rather than individual records? 如何查找Core Data持久存储的大小以及文件系统上的可用空间? - How do I find the size of my Core Data persistent store and the free space on the file system? 如何从 Core Data 中的持久存储中删除所有对象? - How do I delete all objects from my persistent store in Core Data? 如何在Core Data中存储图像? - How to store an image in Core Data? 如何删除核心数据存储区中除一个对象之外的所有对象? - How can I delete all but one object in a Core Data store? iPhone SDK核心数据系统如何将日期类型存储到sqlite? - How does the iPhone SDK Core Data system store date types to sqlite? 我应该如何在核心数据数据库中存储UIImages? - How should I store UIImages within my Core Data database?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM