因此,在3D Touch监控控制器中,我在设置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]
}()
在委托控制器中,我有此方法来呈现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)
}
一切正常,我可以与任何应用共享,并且可以使用文件浏览器进行存储,但是如果我按“保存图像”,则应用崩溃。看起来我需要画廊许可,但我不知道如何在UIPreviewAction中实现它以检查是否具有许可,以便保存该许可,是否不许可。>
答案 0 :(得分:1)
在新的InfoPlist.strings文件中添加新记录。
<key>NSPhotoLibraryAddUsageDescription</key>
<string>$(PRODUCT_NAME)</string>
<key>NSPhotoLibraryUsageDescription</key>
<string>$(PRODUCT_NAME)</string>
从模拟器中删除您的应用,然后再次运行。