问题
I know you can save one by using the binary data property of an entity but is it possible to save multiple?
you can save one by using this:
UIImageJPEGRepresentation(image: UIImage)
you can save it and then transform it into an image again with
UIImage(data: ____)
but can I transform an array full of images into binary data?
If you can't with core data, any other ways I can do it?
BTW, the image array is full of user taken image saved on the device..
Thanks for all the helpers!
回答1:
convert your array to NSMutableArray
and archive it as NSData
var imgArray = [UIImage]();
var CDataArray = NSMutableArray();
for img in imgArray{
let data : NSData = NSData(data: UIImagePNGRepresentation(img))
CDataArray.addObject(data);
}
//convert the Array to NSData
//you can save this in core data
var coreDataObject = NSKeyedArchiver.archivedDataWithRootObject(CDataArray);
after pulling from CData, extract data:
//extract:
if let mySavedData = NSKeyedUnarchiver.unarchiveObjectWithData(coreDataObject) as? NSArray{
//extract data..
}
Swift 5
There are some methods deprecated now. The following code is working in Swift 5:
func coreDataObjectFromImages(images: [UIImage]) -> Data? {
let dataArray = NSMutableArray()
for img in images {
if let data = img.pngData() {
dataArray.add(data)
}
}
return try? NSKeyedArchiver.archivedData(withRootObject: dataArray, requiringSecureCoding: true)
}
func imagesFromCoreData(object: Data?) -> [UIImage]? {
var retVal = [UIImage]()
guard let object = object else { return nil }
if let dataArray = try? NSKeyedUnarchiver.unarchivedObject(ofClass: NSArray.self, from: object) {
for data in dataArray {
if let data = data as? Data, let image = UIImage(data: data) {
retVal.append(image)
}
}
}
return retVal
}
回答2:
Based on daniel-krom's answer I created a swift extension to make it easy to add to model objects:
let coreDataObject = imgArray.coreDataRepresentation()
if let retrievedImgArray = coreDataObject?.imageArray() {
// use retrievedImgArray
}
Here's the extension
import UIKit
typealias ImageArray = [UIImage]
typealias ImageArrayRepresentation = Data
extension Array where Element: UIImage {
// Given an array of UIImages return a Data representation of the array suitable for storing in core data as binary data that allows external storage
func coreDataRepresentation() -> ImageArrayRepresentation? {
let CDataArray = NSMutableArray()
for img in self {
guard let imageRepresentation = UIImagePNGRepresentation(img) else {
print("Unable to represent image as PNG")
return nil
}
let data : NSData = NSData(data: imageRepresentation)
CDataArray.add(data)
}
return NSKeyedArchiver.archivedData(withRootObject: CDataArray)
}
}
extension ImageArrayRepresentation {
// Given a Data representation of an array of UIImages return the array
func imageArray() -> ImageArray? {
if let mySavedData = NSKeyedUnarchiver.unarchiveObject(with: self) as? NSArray {
// TODO: Use regular map and return nil if something can't be turned into a UIImage
let imgArray = mySavedData.flatMap({
return UIImage(data: $0 as! Data)
})
return imgArray
}
else {
print("Unable to convert data to ImageArray")
return nil
}
}
}
来源:https://stackoverflow.com/questions/32155030/can-you-save-an-array-of-images-in-core-data