将 UIImage 保存到核心数据中; NSValueTransformers 的问题
我正在尝试将从 iphone 相机拍摄的照片保存到核心数据中。
我有从相机拍摄的 UIImage。之后,我不知道我应该做什么。
每张图片都与一个问题实体相关联。现在问题实体没有图像字段。它与图像实体有关系。这样每个问题都可以有许多与之关联的 UIImage。
所以我创建了一个问题实体。现在,如何将 UIImage 保存到核心数据中到图像实体中,以及如何将其与问题实体连接起来?我需要创建一个图像实体吗?我创建了一个 ImageToDataTransformer,但我不知道谁调用它 - 核心数据还是我?在我的 xcdatamodel 中,我有一个图像实体。我在那里放入 ImageToDataTransformer 。
我知道我应该将 url 存储到 UIImage,但我想了解如何存储图像。这对我来说似乎有点令人困惑。
谢谢
Question *question = [NSEntityDescription insertNewObjectForEntityForName:@"Question" inManagedObjectContext:managedObjectContext];
question.date = [NSDate date];
ImageToDataTransformer *transformer = [[ImageToDataTransformer alloc]init];
NSData *imageData = [transformer transformedValue:mm];//mm is an id, but it is the UIImage saved from the camera.
question.image = imageData;
I am trying to save a pic taken from the iphone camera to core data.
I have the UIImage taken from the camera. AFter that, I don't know what I'm supposed to do.
Each pic is associated with a question entity. Now a question entity does not have an image field. It has a relationship to an Image entity. This is so that each question can have many UIImages associated with it.
So I have created a question entity. Now how do I save the UIImage into core data into an Image entity, and how do I join it with a question entity? Do I need to create an Image entity? I have created an ImageToDataTransformer, but I do not know who calls that--core data or me? In my xcdatamodel, I have an Image entity. I put in ImageToDataTransformer there.
I am aware that I should be storing urls to the UIImage, but I want to understand how to store images. It seems kind of confusing to me.
Thanks
Question *question = [NSEntityDescription insertNewObjectForEntityForName:@"Question" inManagedObjectContext:managedObjectContext];
question.date = [NSDate date];
ImageToDataTransformer *transformer = [[ImageToDataTransformer alloc]init];
NSData *imageData = [transformer transformedValue:mm];//mm is an id, but it is the UIImage saved from the camera.
question.image = imageData;
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我建议您创建一个 QuestionImage 实体(或类似的名称)并为其指定一个名为 path 的字符串属性。此路径将存储磁盘上您之前写出图像的位置。将图像放置在文档文件夹中将确保它不会被任何缓存转储清除。在 QuestionImage 实体(或任何您命名的实体)的实现中,您可以实现prepareForDeletion 并从磁盘中删除图像,以确保没有任何未引用的图像挂在周围。
在 Core Data 中存储二进制数据可能很麻烦,应尽可能避免。
I would suggest you make a QuestionImage entity (or something named similar) and give it a single string attribute named path. This path will store a location on disk where you've previously written out your image. Placing the image in the documents folder will ensure it does not get cleared with any cache dumps. In the implementation of your QuestionImage entity (or whatever you name it) you can implement prepareForDeletion and remove the image from the disk to make sure you don't have any un-referenced images hanging around.
Storing binary data in Core Data can be troublesome, and should be avoided where possible.