美文网首页
Swift 图片保存,分享,包括权限判断

Swift 图片保存,分享,包括权限判断

作者: wenju | 来源:发表于2021-07-31 16:38 被阅读0次

保存图片

//询问权限
        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)
                    }
                }
            })
        })

相关文章

网友评论

      本文标题:Swift 图片保存,分享,包括权限判断

      本文链接:https://www.haomeiwen.com/subject/eqkkvltx.html