【问题标题】:Cannot get URL for photo saved to PhotosAlbum in Swift无法在 Swift 中获取保存到 PhotosAlbum 的照片的 URL
【发布时间】:2016-08-17 05:30:09
【问题描述】:

我对 Swift 很陌生,我只是想获取我保存的照片的文件 URL。下面是一个小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
    }
})

但是,我一辈子都无法建造它。它显示以下错误:

无法使用参数列表调用“writeImageToSavedPhotosAlbum” 类型'(CGImage?,方向:ALAssetOrientation,completionBlock:(NSURL!, NSError!)-> 无效)'

它表明它期望:

需要一个类型为 '(CGImage!,orientation: ALAssetOrientation,完成块: ALAssetsLibraryWriteImageCompletionBlock!)'

我可以看到一个明显的问题是 CGImage 参数,但我不知道如何更改它,或者如果这是唯一的问题。有没有人看到这个或看到我的菜鸟错误是什么?

【问题讨论】:

    标签: swift


    【解决方案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 的链接,其中包含您正在尝试执行的操作的相关部分。

    【讨论】:

    • 对,但是当我打开可选的包装时,我没有得到额外的!。我仍然看到错误: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!)'
    • 我想建议的一件事是切换到新的照片框架。自 iOS 9 起,资产库已被弃用。这是链接 developer.apple.com/library/ios/documentation/Photos/Reference/…
    • 这只是一个概念证明,鉴于我已经很接近了,我真的很想看看我是否可以让它工作。我不担心这是在生产中。非常感谢您的链接。如果我无法解决这个问题,我肯定会将您的答案标记为完整并继续使用照片框架
    • 您需要在ALAssetsLibrary 的实例上调用此方法,而不是在类本身上。对不起,我没有立即抓住那个。 @gordysc
    • 你是我的英雄!你不知道这救了我多少!谢谢!!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-05-26
    • 1970-01-01
    • 1970-01-01
    • 2017-10-11
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多