0

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?

Community
  • 1
  • 1
gordysc
  • 234
  • 1
  • 4
  • 22

1 Answers1

1

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.

Morgan Wilde
  • 16,795
  • 10
  • 53
  • 99
  • Right, but when I unwrap the optional, I don't get the additional !. I'm still seeing the error: `Cannot invoke 'writeImageToSavedPhotosAlbum' with an argument list of type '(CGImage, orientation: ALAssetOrientation, completionBlock: (NSURL!, NSError!) -> Void)'` And it still complains: `Expected an argument list of type '(CGImage!, orientation: ALAssetOrientation, completionBlock: ALAssetsLibraryWriteImageCompletionBlock!)'` – gordysc Aug 17 '16 at 05:46
  • One thing I'd like to suggest is to switch to the new Photos Framework. Asset Library has been deprecated as of iOS 9. Here's the link https://developer.apple.com/library/ios/documentation/Photos/Reference/Photos_Framework/index.html#//apple_ref/doc/uid/TP40014408 – Morgan Wilde Aug 17 '16 at 05:49
  • This is just for a proof of concept and given I'm so close, I'd really like to see if I can just get this to work. I'm not worried about this being in production. Thank you very much for the link though. If I can't get past this I'll definitely mark your answer as complete and proceed with the Photos Framework – gordysc Aug 17 '16 at 05:53
  • You need to call this method on an instance of `ALAssetsLibrary`, and not on the class itself. Sorry I didn't catch that one immediatelly. @gordysc – Morgan Wilde Aug 17 '16 at 06:01
  • 1
    You are my hero! You have no idea how much this saved me! Thank you!! – gordysc Aug 17 '16 at 06:09
  • No problem! Just transition to the new framework when you have the chance, it'll save you a lot of trouble down the line. – Morgan Wilde Aug 17 '16 at 06:12