2016-08-17 58 views
0

当谈到Swift时,我很绿,而我只是想获取我保存的照片的文件URL。下面是一个小的snippet inspired from this StackOverflow question在Swift中无法获取保存到PhotosAlbum的照片的URL

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 
    } 
}) 

但是,我不能为我的生活得到它建立。它显示了以下错误:

Cannot invoke 'writeImageToSavedPhotosAlbum' with an argument list of type '(CGImage?, orientation: ALAssetOrientation, completionBlock: (NSURL!, NSError!) -> Void)'

而且它指出,它预计:

Expected an argument list of type '(CGImage!, orientation: ALAssetOrientation, completionBlock: ALAssetsLibraryWriteImageCompletionBlock!)'

的一个明显的问题,我可以看到这关是CGImage参数,但我不知道如何改变这种状况,或者如果这是唯一的问题。有没有人看过这个,看看我的新手错误是什么?

回答

1

您有一个可选的image.CGImage,错误表示您必须拆开它。

这应该工作。

let library = ALAssetsLibrary() 
library.writeImageToSavedPhotosAlbum(image.CGImage!, orientation: orientation, completionBlock: { 
    url, error in 
    if error != nil { 
     print(error) 
    } else { 
     // We have the URL here 
    } 
}) 

新的Photos.framework确实提供了您正在寻找的功能。以下是developer library的链接,其中包含您正在尝试执行的相关部分。

+0

对,但是当我打开可选的时候,我没有得到额外的!我仍然看到错误: '无法用类型为'(CGImage,orientation:ALAssetOrientation,completionBlock:(NSURL !, NSError!) - > Void)'的参数列表调用'writeImageToSavedPhotosAlbum''' 它仍然抱怨: '预期类型的​​参数列表'(CGImage !, orientation:ALAssetOrientation,completionBlock:ALAssetsLibraryWriteImageCompletionBlock!)'' – gordysc

+0

我想建议的一件事是切换到新的Photos Framework。资产库从iOS 9开始已被弃用。链接https://developer.apple.com/library/ios/documentation/Photos/Reference/Photos_Framework/index.html#//apple_ref/doc/uid/TP40014408 –

+0

此内容只是为了一个概念的证明,并且鉴于我如此接近,我真的很想看看我是否可以让这个工作。我并不担心这种生产。尽管非常感谢你的链接。如果我无法超越这一点,我一定会将您的答案标记为完整并继续使用“照片框架” – gordysc