【问题标题】:Saving to User Photo Library Silently Fails静默保存到用户照片库失败
【发布时间】:2017-07-01 19:03:56
【问题描述】:

我试图使用以下代码将图像保存到我 iPhone 上的用户相机胶卷:

UIImageWriteToSavedPhotosAlbum(uiImage, nil, nil, nil)

但尽管没有崩溃或出错,它还是失败了。我确保图像是有效的(不是零)UIImage,它确实是。然后我尝试像这样实现完成处理程序:

UIImageWriteToSavedPhotosAlbum(uiImage, self, #selector(self.image(_:didFinishSavingWithError:contextInfo:)), nil)

// ....

@objc func image(_ image: UIImage, didFinishSavingWithError error: Error?, contextInfo: UnsafeRawPointer) {
    if error != nil {
        print("ERROR! ", error!.localizedDescription)
    }
}

但这仍然没有给我一个错误。最后,我尝试在模拟器上运行。这给了我以下错误:

Connection to assetsd was interrupted or assetsd died

Error! Write failed

是什么导致这个失败?

【问题讨论】:

    标签: ios swift uiimage


    【解决方案1】:

    我遇到了this link,它说为了运行UIImageWriteToSavedPhotosAlbum,UIImage“必须支持 CGImage”。因此,为了解决这个问题,我编写了以下代码将我的 UIImage 转换为 CIImage,然后转换为 CGImage,然后再转换回 UIImage:

    let ciImage = self.image!.ciImage
    let context = CIContext()
    let cgImage = context.createCGImage(ciImage!, from: ciImage!.extent)
    let uiImage = UIImage(cgImage: cgImage!)
    
    UIImageWriteToSavedPhotosAlbum(uiImage, self, #selector(self.image(_:didFinishSavingWithError:contextInfo:)), nil)
    

    这终于按预期工作并保存到相机胶卷!

    【讨论】:

    • 这是正确答案。我遇到了这个问题。根据之前使用 CG+CI 的经验,我预计它与 CI-Backed 与 CG-Backed 有关。
    猜你喜欢
    • 1970-01-01
    • 2015-11-03
    • 1970-01-01
    • 1970-01-01
    • 2015-03-10
    • 1970-01-01
    • 2015-10-20
    • 2016-03-13
    • 1970-01-01
    相关资源
    最近更新 更多