【问题标题】:How to allow the user to preview an image before saving and give them the ability to cancel如何允许用户在保存之前预览图像并赋予他们取消的能力
【发布时间】:2020-02-20 08:48:21
【问题描述】:

我正在使用它来存储图像:

 var screenshot: UIImage {
  return UIGraphicsImageRenderer(size: bounds.size).image { _ in
   drawHierarchy(in: CGRect(origin: .zero, size: bounds.size), afterScreenUpdates: true)
  }
 }

 UIImageWriteToSavedPhotosAlbum(view.screenshot, nil, nil, nil)

这会将图像保存到照片中,但我希望允许用户在保存之前查看照片,并为他们提供取消选项。

【问题讨论】:

  • 创建一个PreviewViewController 并将您的图像传递给该控制器以将其显示给用户。添加save and cancel 按钮。单击保存按钮时,调用UIImageWriteToSavedPhotosAlbum 并单击取消按钮时,不要执行任何操作并关闭控制器。

标签: ios swift uiimage


【解决方案1】:
extension RPPreviewViewController {

 convenience init(image: UIImage) {
  self.init()

  view = UIImageView(image: image)
  view.isUserInteractionEnabled = true

  let cancel = UIButton(frame: CGRect(x: 0, y: 0, width: 100, height: 45))
  cancel.setTitleColor(.white, for: UIControl.State.normal)
  cancel.setTitle("Cancel", for: UIControl.State.normal)
  cancel.backgroundColor = UIColor.red
  cancel.addTarget(self, action: #selector(canceled), for: .touchUpInside)

  view.addSubview(cancel)
 }


 @objc func canceled() {
  print("cancel")
 }

}

使用方法:

let preview = RPPreviewViewController(image: view.screenshot)
present(preview, animated: true, completion: nil)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-02-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多