What is the way to store data (images)? NSData, String, or Transformable

I worked with base64 encoding. I successfully encoded the images to NSString from NSData, I also decoded it back to NSData.

So right now I want to store images in Core Data. But would it be better to store NSString, NSData or a third transformable?

The reason I convert images to NSString is because I want to save it in XML too.

Thanks in advance.

+4
source share
4 answers

Use NSValueTransformer to convert from your image to an NSData object, and then save the data block to Core Data. You can register your subclass of transformer value in the simulation tool. You can check an example of Apple PhotoLocations or this tutorial shows how.

Edit completeness: as others have indicated that a data block that is too large can cause performance problems. As @Jesse noted, iOS5 has an optimization where, if the drop of data is too large, then Core Data will save it outside the permanent storage. If you need to target pre-iOS5 and the image is too large, you must save the file somewhere in the sandbox and save the URL in the master data repository. A good discussion at the Apple Dev Forums is here and discusses the limits of data storage in the underlying data.

Luck

+7
source

Transformable (according to @timthetoolman) is the easiest way. However, if your images are large, with iOS 5, the right way to do this is to use the Binary Data attribute type and select Allow External Storage so that Core Data can store a large amount of data outside the database. It can be much more effective.

+8
source

I don’t think you should store them in the master data, I tried this with images once and found that it is too slow. You need to store the locations of the images in the master data, but just write the images to a file. You can do it as follows:

// JPEG [UIImageJPEGRepresentation(image, 1.0) writeToFile:jpgPath atomically:YES]; // PNG [UIImagePNGRepresentation(image) writeToFile:pngPath atomically:YES]; 
+1
source

Target iOS5 or later - Performing Saving an image becomes trivial

New versions of Xcode and iOS now make storing images both easy and efficient. You no longer have to choose to store images in the main data for convenience or in the file system for performance - Core Data will take care of this for you.

  • UIImage now matches NSCoding in iOS 5. If you can target iOS 5 and later, you can simply set the managed entity attribute as Transformable and execute.
  • if you select the "Allow external storage" checkbox, Core Data will decide whether to store the BLOB in a managed object or as an external file - everything is transparent to the developer. so that large images are saved outside your master data warehouse.
+1
source

Source: https://habr.com/ru/post/1383222/


All Articles