Reputation: 81
So, in the 3D Touch peek controller, I have this computed var where I setup the UIPreviewAction:
override lazy var previewActionItems: [UIPreviewActionItem] = {
let save = UIPreviewAction.init(title: "Share Image", style: UIPreviewAction.Style.default, handler: { (action, controller) in
let renderer = UIGraphicsImageRenderer(size: self.view.bounds.size)
let imageAux = renderer.image { ctx in
self.view.drawHierarchy(in: self.view.bounds, afterScreenUpdates: true)
}
self.delegate!.shareImage(image: imageAux, url: self.data.url)
})
return [save]
}()
and in the delegate controller I have this method to present the UIActivityViewController:
func shareImage(image: UIImage, url: String) {
if let data = image.pngData() {
let paths = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask)
let filename = paths[0].appendingPathComponent(url)
try? data.write(to: filename)
}
let activityViewController = UIActivityViewController(activityItems: [image] , applicationActivities: nil)
activityViewController.popoverPresentationController?.sourceView = self.view
self.present(activityViewController, animated: true, completion: nil)
}
everything works fine, i can share with any apps, and can store using the File Explore, but if I press "Save Image", the app crashes. Looks like I need gallery permission, but I don't know how to implement it in the UIPreviewAction to check if I have the permission or not, so that if I do it saves, and if I do not it asks for permission.
Upvotes: 1
Views: 1024
Reputation:
Add new records in your new InfoPlist.strings file.
<key>NSPhotoLibraryAddUsageDescription</key>
<string>$(PRODUCT_NAME)</string>
<key>NSPhotoLibraryUsageDescription</key>
<string>$(PRODUCT_NAME)</string>
delete your app from simulator and run again.
Upvotes: 2