ios - "Saved photo"在用户允许照片库权限之前弹出警报?

标签 ios swift photo

当用户点击该按钮时,它会立即触发“照片已保存”警告,然后再询问用户许可。如果用户单击否,则照片不会保存,但仍会显示警告。是否有一个 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/

相关文章:

ios - 如何在图形设计器中设计 SKNode 并在代码中重用它?

ios:如何在特定位置启动光标?

ios - 用于发布视频的 Instagram iOS API

ios - UIViewController 部分标题未显示

ios - 如何以编程方式使 UITableView 的第一部分出现在距顶部边框一定距离处?

ios - Xcode iOS 照片库应用程序

ios - 从字符串swift 3中获取特定范围的字符

ios - 为什么 AVAsset 状态变化的观察者不起作用?

photo - iOS 8 photoKit 如何使用 photoKit 编辑图像

excel - 使用VBA根据Excel中的点名称自动将照片添加到圆环图点