How To Save An Image Using DKIMage

Here’s a quick snippet to save the original image once it has been selected usingĀ DKImage.

 

self.assets![0].fetchOriginalImageWithCompleteBlock({ (image, info) in
    let phAsset = self.assets![0].originalAsset
    self.savePHAsset(asset: phAsset!){(file) -> Void in
        print(file)
    }
})
func savePHAsset(asset: PHAsset, completionHandler: @escaping ((URL) -> Void)) {
let manager = PHImageManager.default()
let options = PHImageRequestOptions()
options.version = .original
options.isSynchronous = true
 
manager.requestImageData(for: asset, options: options) { data, _, _, _ in
 
if let data = data {
let documentsPath = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true)[0] as String
 
let image = UIImage(data: data)
self.phaAsset = UIImage(data: data)
self.imageData = UIImageJPEGRepresentation(image!,0.3) as NSData!
self.fileNombre = "\(self.currentTag!)-\(randomStringWithLength(len: 5)).jpg"
self.filename[self.currentTag!] = FileName(name: self.fileNombre)
 
do {
let nsU = URL(fileURLWithPath:"\(documentsPath)/\(self.fileNombre)")
try self.imageData.write(to: nsU, options: [])
completionHandler(nsU)
} catch {
print("Error with \(documentsPath)/\(self.fileNombre)")
}
 
}
}
}

 

webjunkie01

 

Leave a Reply

Your email address will not be published. Required fields are marked *