我试图使用以下代码将图像保存到我 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)
}
}
但这仍然没有给我报错。最后,我尝试在模拟器上运行。然后这给了我以下错误:
与 assetsd 的连接中断或 assetsd 终止
错误!写入失败
导致失败的原因是什么?
最佳答案
我遇到了 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)
这终于奏效并按预期保存到相机胶卷!
关于ios - 保存到用户照片库静默失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44864432/