我正在用它来存储图像:
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)
这会将图像保存到照片,但是我想允许用户在保存之前先查看照片,并为他们提供取消的选项。
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)