本文介绍了核心数据 - 存储图像(iPhone)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个应用程序,允许用户为其帐户添加图片。

I have an application where I allow the user to add an image for their account.

我想知道如何存储图片(从相机或照片库中获取)使用Core Data ,因为我可以提供备份

I wish to know how to store an image (obtained from the camera or photo library) using Core Data, as I may offer a backup facility for the user and would be looking to transfer the image data to a server.

我遇到了创建一个图像的设计建议。 / code>模型对象,并与 User 模型对象创建1对1关系(以便相关的Image对象不被调用,除非需要)。但是,我不确定如何实际存储图像,以及这是否在性能方面可能致命。

I have come across the design suggestion of creating an Image model object and creating a 1-to-1 relationship with the User model object (so that the associated Image object is not called up unless required). However, I am unsure how to practically store the image and whether this is potentially fatal in terms of performance.

我会感谢任何关于方法和陷阱的建议

I would be grateful for any advice on the approach and pitfalls from anyone who has attempted this.

推荐答案

在Core Data中存储图像数据的规则如下:

The rule for storing image data in Core Data is as follows:


  • < 100 kb存储在相关实体(个人,地址等)中。

  • < 1 mb存储在关系的另一端的单独实体中,以避免性能问题。

  • < 100 kb store in the related entity (person, address, whatever).
  • < 1 mb store in a separate entity on the other end of a relationship to avoid performance issues.


您可以使用可变数据类型存储NSImage直接进入Core Data。事实上,你可以使用可变数据类型来存储实现NSCoder协议的任何东西。

You can use the transformable data type to store the NSImage directly into Core Data. In fact you can use the transformable data type to store anything that implements the NSCoder protocol.

我个人不会把它转换为一个CGImageRef,因为你可以失去很多信息那样。

Personally I would not convert it to a CGImageRef as you can lose a lot of information that way.

这篇关于核心数据 - 存储图像(iPhone)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-14 11:48