保存图片
//询问权限
PHPhotoLibrary.requestAuthorization({ status in
DispatchQueue.main.async(execute: {
if status == .authorized {
//保存Image
UIImageWriteToSavedPhotosAlbum(self.downlaodImage, self, #selector(self.saveImage(image:didFinishSavingWithError:contextInfo:)), nil)
}else{
let alertController = UIAlertController(title: nil,
message: "Download failed, please check permissions", preferredStyle: .alert)
//显示提示框
self.present(alertController, animated: true, completion: nil)
//两秒钟后自动消失
DispatchQueue.main.asyncAfter(deadline: DispatchTime.now() + 2) {
self.presentedViewController?.dismiss(animated: false, completion: nil)
}
}
})
})
//保存图片监听
@objc func saveImage(image:UIImage,didFinishSavingWithError error:NSError?,contextInfo:AnyObject) {
if error != nil {
let alertController = UIAlertController(title: nil,
message: "Download failed, please check permissions", preferredStyle: .alert)
//显示提示框
self.present(alertController, animated: true, completion: nil)
} else {
let alertController = UIAlertController(title: nil,
message: "download successful", preferredStyle: .alert)
//显示提示框
self.present(alertController, animated: true, completion: nil)
}
//两秒钟后自动消失
DispatchQueue.main.asyncAfter(deadline: DispatchTime.now() + 2) {
self.presentedViewController?.dismiss(animated: false, completion: nil)
}
}
分享图片
PHPhotoLibrary.requestAuthorization({ status in
DispatchQueue.main.async(execute: { [self] in
if status == .authorized {
let imageToShare = [self.downlaodImage]
let activityViewController = UIActivityViewController(activityItems: imageToShare as [Any] , applicationActivities: nil)
activityViewController.popoverPresentationController?.sourceView = self.view
activityViewController.excludedActivityTypes = [ UIActivity.ActivityType.airDrop, UIActivity.ActivityType.postToFacebook ]
self.present(activityViewController, animated: true, completion: nil)
}else{
let alertController = UIAlertController(title: nil,
message: "Share failed, please check permissions", preferredStyle: .alert)
//显示提示框
self.present(alertController, animated: true, completion: nil)
//两秒钟后自动消失
DispatchQueue.main.asyncAfter(deadline: DispatchTime.now() + 2) {
self.presentedViewController?.dismiss(animated: false, completion: nil)
}
}
})
})
网友评论