xcode Which way to store data(image)? NSData, String or Transformable
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8280891/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Which way to store data(image)? NSData, String or Transformable
提问by WYS
I have been working with base64 encoding. I have successfully encoded images to NSString from NSData, I have also decoded it back to NSData.
I have been working with base64 encoding. I have successfully encoded images to NSString from NSData, I have also decoded it back to NSData.
So right now I want to store images in Core Data. But would it be best to store an NSString, NSData or the third transformable?
So right now I want to store images in Core Data. But would it be best to store an NSString, NSData or the third transformable?
The reason I convert images to NSString is because I want to store it in XML too.
The reason I convert images to NSString is because I want to store it in XML too.
Thanks in advance.
Thanks in advance.
回答by Jesse Rusak
Transformable (as per @timthetoolman) is the easiest way. If your images are large, though, as of iOS 5, the right way to do this is to use the "Binary Data" attribute type and choose "Allows External Storage" so that Core Data can store the large blob of data outside of the database. This can be much more efficient.
Transformable (as per @timthetoolman) is the easiest way. If your images are large, though, as of iOS 5, the right way to do this is to use the "Binary Data" attribute type and choose "Allows External Storage" so that Core Data can store the large blob of data outside of the database. This can be much more efficient.
回答by timthetoolman
Use an NSValueTransformer to convert from your image to an NSData object and then store the data blob in Core Data. You can register your value transformer subclass in the modeling tool. You can check out Apple's PhotoLocations example or this tutorialshows how.
Use an NSValueTransformer to convert from your image to an NSData object and then store the data blob in Core Data. You can register your value transformer subclass in the modeling tool. You can check out Apple's PhotoLocations example or this tutorialshows how.
Edit for completeness: as others have pointed out too large a data blob will cause performance issues. As pointed out by @Jesse, iOS5 has an optimization where if the data blob is too large, then Core Data will store it outside of the persistent store. If you have to target pre-iOS5 and the image is too large then you should save the file somewhere in the sandbox and store the URL in the Core Data store. A good discussion in the Apple Dev Forums is hereand discusses the limits of storing data blobs in Core Data.
Edit for completeness: as others have pointed out too large a data blob will cause performance issues. As pointed out by @Jesse, iOS5 has an optimization where if the data blob is too large, then Core Data will store it outside of the persistent store. If you have to target pre-iOS5 and the image is too large then you should save the file somewhere in the sandbox and store the URL in the Core Data store. A good discussion in the Apple Dev Forums is hereand discusses the limits of storing data blobs in Core Data.
Good Luck
Good Luck
回答by Michael Frederick
I don't think you should store them in core data, I tried that with images once and found it to be too slow. You should store the locations of the images in core data but just write the images to a file. You can do that as follows:
I don't think you should store them in core data, I tried that with images once and found it to be too slow. You should store the locations of the images in core data but just write the images to a file. You can do that as follows:
// JPEG
[UIImageJPEGRepresentation(image, 1.0) writeToFile:jpgPath atomically:YES];
// PNG
[UIImagePNGRepresentation(image) writeToFile:pngPath atomically:YES];
回答by memmons
Target iOS5 or later -- Performant Image storage becomes trivial
Target iOS5 or later -- Performant Image storage becomes trivial
Newer versions of Xcode and iOS now make storing images both easy and performant. You no longer have to choose to store your images in core data for convenience or in the file system for performance -- Core Data will take care of it for you.
Newer versions of Xcode and iOS now make storing images both easy and performant. You no longer have to choose to store your images in core data for convenience or in the file system for performance -- Core Data will take care of it for you.
UIImage
now conforms toNSCoding
in iOS 5. If you're able to target iOS 5 and later, you can just set the the managed object's attribute asTransformable
and be done.- if you check the "Allows External Storage" option, Core Data will make the decision whether to store the BLOB in the managed object or as an external file -- all transparently to the developer. to cause larger images to be saved outside of your Core Data store
UIImage
now conforms toNSCoding
in iOS 5. If you're able to target iOS 5 and later, you can just set the the managed object's attribute asTransformable
and be done.- if you check the "Allows External Storage" option, Core Data will make the decision whether to store the BLOB in the managed object or as an external file -- all transparently to the developer. to cause larger images to be saved outside of your Core Data store