当用户点击该按钮时,它会立即触发“照片已保存”警告,然后再询问用户许可。如果用户单击否,则照片不会保存,但仍会显示警告。是否有一个 if else 语句可以用来让它在用户允许访问照片库之前不会弹出警报?
@IBAction func savePhotoClicked(_ sender: Any) {
UIImageWriteToSavedPhotosAlbum(image, nil, nil, nil)
let alert = UIAlertController(title: "Saved!", message: "This wallpaper has been saved.", preferredStyle: .alert)
let okAction = UIAlertAction(title: "Ok", style: .default, handler: nil)
alert.addAction(okAction)
self.present(alert, animated: true, completion: nil)
}
最佳答案
您过早地展示了警报 Controller 。对 UIImageWriteToSavedPhotosAlbum
的调用是异步的。看到您传递给第二个、第三个和第四个参数的所有那些 nil
值了吗?将它们替换为适当的值,这样您就可以在调用 UIImageWriteToSavedPhotosAlbum
实际完成时调用警报,并且您可以正确确定图像是否实际保存。
@IBAction func savePhotoClicked(_ sender: Any) {
UIImageWriteToSavedPhotosAlbum(image, self, #selector(image(_:didFinishSavingWithError:contextInfo:)), nil)
}
@objc func image(_ image: UIImage, didFinishSavingWithError error: Error?, contextInfo: UnsafeRawPointer) {
if let error = error {
// show error
} else {
let alert = UIAlertController(title: "Saved!", message: "This wallpaper has been saved.", preferredStyle: .alert)
let okAction = UIAlertAction(title: "Ok", style: .default, handler: nil)
alert.addAction(okAction)
self.present(alert, animated: true, completion: nil)
}
}
关于ios - "Saved photo"在用户允许照片库权限之前弹出警报?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54790516/