简体   繁体   English

UIImagePickerController-如何将图像保存到核心数据?

[英]UIImagePickerController - how to save image to core data?

I have an app, which uses the UIImagePicker to let the use take a photo or choose a photo from the gallery. 我有一个应用程序,它使用UIImagePicker允许用户拍照或从图库中选择照片。 The picking and displaying works so far, but I need some guidance what to do and how next. 到目前为止,挑选和显示工作正常,但是我需要一些指导以做什么以及下一步如何做。 Here's my delegate method: 这是我的委托方法:

- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {

    UIImage *chosenImage = info[UIImagePickerControllerEditedImage];
    self.userImageView.image = chosenImage;

    [picker dismissViewControllerAnimated:YES completion:NULL];    
}

So, the captured/selected image is displayed nicely in an UIImageView . 因此,捕获/选择的图像可以很好地显示在UIImageView

What is the best way to save it? 最好的保存方法是什么? Save the image itself to Core Data, or just the URL in the filesystem? image本身保存到核心数据,还是仅保存到文件系统中的URL How can I obtain the URL in the filesystem? 如何获取文件系统中的URL? Does it matter, if the user took the photo, or select it from the gallery? 用户拍了照片还是从图库中选择照片有关系吗? Any hints, ideas appreciated, thanks. 任何提示,想法表示赞赏,谢谢。

You can save image in core data by converting it to NSData object. 您可以通过将image转换为NSData对象将其保存在核心数据中。 And when you want to show it back to UIImageView you have to convert it back to UIImage Object. 当您想将其显示回UIImageView ,必须将其转换回UIImage对象。 You can do this easily by this way. 您可以通过这种方式轻松地做到这一点。

NSData* data = UIImageJPEGRepresentation(myImage, COMPRESSION_QUALITY);

And by this way you can convert data back to image 通过这种方式,您可以将数据转换回图像

UIImage *image = [UIImage imageWithData:data];

This was the way to convert image to data and vice versa. 这是将image转换为data ,反之亦然。 Where you should save this image will depend on size and requriement. 该图像的保存位置将取决于大小和要求。

You can save image to core data but that will make your core data object to consume more memory. 您可以将image保存到核心数据,但这将使您的核心数据对象消耗更多的内存。 If you still want to save image in core data create seprate entity for image and and create a reference for the image in othere entity, in this way by faulting will save some memory. 如果您仍想将图像保存在核心数据中,请为image创建一个单独的实体,并在另一个实体中创建该图像的引用,这样通过故障处理将节省一些内存。

You can also save image in file-system and save there urls to core data as strings. 您也可以将图像保存在文件系统中,并将其中的核心数据URL保存为字符串。

According to Apple docs : ( https://developer.apple.com/library/mac/documentation/Cocoa/Conceptual/CoreData/Articles/cdPerformance.html ) 根据Apple文档:( https://developer.apple.com/library/mac/documentation/Cocoa/Conceptual/CoreData/Articles/cdPerformance.html

Large Data Objects (BLOBs) 大数据对象(BLOB)

If your application uses large BLOBs ("Binary Large OBjects" such as image and sound data), you need to take care to minimize overheads. 如果您的应用程序使用大型BLOB(“二进制大型对象”,例如图像和声音数据),则需要注意使开销最小化。 The exact definition of “small”, “modest”, and “large” is fluid and depends on an application's usage. “小”,“适中”和“大”的确切定义是不确定的,并取决于应用程序的用法。 A loose rule of thumb is that objects in the order of kilobytes in size are of a “modest” sized and those in the order of megabytes in size are “large” sized. 一个宽松的经验法则是,大小为千字节的对象的大小为“适中”,而大小为兆字节的对象的大小为“大”。 Some developers have achieved good performance with 10MB BLOBs in a database. 一些开发人员使用数据库中的10MB BLOB取得了良好的性能。 On the other hand, if an application has millions of rows in a table, even 128 bytes might be a "modest" sized CLOB (Character Large OBject) that needs to be normalized into a separate table. 另一方面,如果应用程序在一个表中有数百万行,则即使128个字节也可能是“中等”大小的CLOB(字符大对象),需要将其标准化为单独的表。

In general, if you need to store BLOBs in a persistent store, you should use an SQLite store. 通常,如果需要将BLOB存储在持久性存储中,则应使用SQLite存储。 The XML and binary stores require that the whole object graph reside in memory, and store writes are atomic (see “Persistent Store Features”) which means that they do not efficiently deal with large data objects. XML和二进制存储要求整个对象图都驻留在内存中,并且存储写操作是原子的(请参阅“持久存储功能”),这意味着它们不能有效地处理大型数据对象。 SQLite can scale to handle extremely large databases. SQLite可以扩展以处理超大型数据库。 Properly used, SQLite provides good performance for databases up to 100GB, and a single row can hold up to 1GB (although of course reading 1GB of data into memory is an expensive operation no matter how efficient the repository). 如果正确使用,SQLite可为高达100GB的数据库提供良好的性能,单行最多可容纳1GB(尽管无论存储库的效率如何,将1GB的数据读取到内存中都是一项昂贵的操作)。

A BLOB often represents an attribute of an entity—for example, a photograph might be an attribute of an Employee entity. BLOB通常表示实体的属性,例如,照片可能是Employee实体的属性。 For small to modest sized BLOBs (and CLOBs), you should create a separate entity for the data and create a to-one relationship in place of the attribute. 对于小到中等大小的BLOB(和CLOB),您应该为数据创建一个单独的实体,并创建一对一关系来代替属性。 For example, you might create Employee and Photograph entities with a one-to-one relationship between them, where the relationship from Employee to Photograph replaces the Employee's photograph attribute. 例如,您可以创建Employee和Photograph实体,它们之间具有一对一的关系,其中从Employee到Photograph的关系将替换Employee的photograph属性。 This pattern maximizes the benefits of object faulting (see “Faulting and Uniquing”). 这种模式最大程度地发挥了对象故障的好处(请参阅“故障和唯一性”)。 Any given photograph is only retrieved if it is actually needed (if the relationship is traversed). 任何给定的照片仅在实际需要时才检索(如果遍历关系)。

It is better, however, if you are able to store BLOBs as resources on the filesystem, and to maintain links (such as URLs or paths) to those resources. 但是,如果能够将BLOB作为资源存储在文件系统上,并能够维护到这些资源的链接(例如URL或路径),则更好。 You can then load a BLOB as and when necessary. 然后,您可以根据需要加载BLOB。

So, depending on the size of the image, you can either save the path or the image in CoreData. 因此,根据图像的大小,您可以将路径或图像保存在CoreData中。

To save your UIImage, you have to transform it in NSData : 要保存UIImage,必须在NSData中对其进行转换:

NSData imageData = UIImagePNGRepresentation(myImage); NSData imageData = UIImagePNGRepresentation(myImage); NSData imageData = UIImageJPEGRepresentation(myImage, QUALITY); NSData imageData = UIImageJPEGRepresentation(myImage,QUALITY);

Then you can save it at a path you choose : 然后可以将其保存在您选择的路径中:

[imageData writeToFile:fullPath atomically:YES]; [imageData writeToFile:fullPath atomic:YES];

I tend to always use path, even for small image, to keep continuity on how my data model works. 我倾向于总是使用路径,即使是小图像,也可以保持数据模型的连续性。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM