核心数据 - 存储图像 (iPhone) [英] Core Data - Storing Images (iPhone)

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

问题描述

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

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

我想知道如何存储图像(从相机或照片库中获得)使用核心数据,因为我可能会为用户提供备份工具并希望传输图像数据到服务器.

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.

我遇到了创建 Image 模型对象并与 User 模型对象创建一对一关系的设计建议(以便关联的 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 存储在关系另一端的单独实体中,以避免性能问题.

在磁盘上存储 1 mb 并引用核心数据存储中的路径.

1 mb store on disk and reference the path in your Core Data store.

您可以使用可转换数据类型将 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)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆