Reputation: 334
I'm very green when it comes to Swift, and I'm just trying to get the file URL for a photo I've saved. Below is a small snippet inspired from this StackOverflow question
let imageData = AVCaptureStillImageOutput.jpegStillImageNSDataRepresentation(imageDataSampleBuffer)
// Save the photo to the album library
let image = UIImage(data: imageData)!
let orientation = ALAssetOrientation(rawValue: image.imageOrientation.rawValue)!
ALAssetsLibrary.writeImageToSavedPhotosAlbum(image.CGImage, orientation: orientation, completionBlock: {
(url: NSURL!, error: NSError!) -> Void in
if error != nil {
print(error)
} else {
// We have the URL here
}
})
However, I can't for the life of me get it to build. It's showing the following error:
Cannot invoke 'writeImageToSavedPhotosAlbum' with an argument list of type '(CGImage?, orientation: ALAssetOrientation, completionBlock: (NSURL!, NSError!) -> Void)'
And it's stating it expects:
Expected an argument list of type '(CGImage!, orientation: ALAssetOrientation, completionBlock: ALAssetsLibraryWriteImageCompletionBlock!)'
The one obvious issue I can see that's off is the CGImage parameter, but I have no clue how to change this, or if that's the only issue. Has anyone seen this or see what my rookie mistake here is?
Upvotes: 0
Views: 416
Reputation: 17295
You have an optional image.CGImage
, the error says that you must unwrap it.
This should work.
let library = ALAssetsLibrary()
library.writeImageToSavedPhotosAlbum(image.CGImage!, orientation: orientation, completionBlock: {
url, error in
if error != nil {
print(error)
} else {
// We have the URL here
}
})
The new Photos.framework does provide the functionality you're looking for. Here's the link to the developer library containing relevant parts to what you're trying to do.
Upvotes: 1