Swift 获取系统用权限 调用相机截图

#import <PhotosUI/PhotosUI.h>

/**
 *  系统权限的获取
 */
class JYSystemAuthorityModel: NSObject {
    
    /// 获取访问相册的权限
    ///
    /// - Parameter result: 权限结果
    static func checkAlbunAuthority(result: @escaping ((_ grantedd: Bool) -> Void )) {
        let phototAuthorityStatus = PHPhotoLibrary.authorizationStatus()
        DispatchQueue.main.async {
            switch phototAuthorityStatus {
            case .authorized:
                result(true)
            case .notDetermined:
                PHPhotoLibrary.requestAuthorization({ (status) in
                    DispatchQueue.main.async {
                        result(status == .authorized)
                    }
                })
            default:
                result(false)
            }
        }
    }
    
    /// 获取摄像头访问权限
    ///
    /// - Parameter result: 权限结果    
    static func checkCamerAuthority(result: @escaping ((_ granted: Bool) -> Void)) {
        let videAuthStatus = AVCaptureDevice.authorizationStatus(for: .video)
        switch videAuthStatus {
        case .authorized:
            result(true)
        case .notDetermined:
            AVCaptureDevice.requestAccess(for: .video) { (res) in
                result(res)
            }
        default:
            result(false)
        }
    }
    
    
    ///  定位权限判断
    ///
    /// - Returns: 是否有权限
    static func checkLocationAuthority() -> Bool {
        let authStatus = CLLocationManager.authorizationStatus()
        return authStatus != .restricted && authStatus != .denied
    }
    
}

1.点击 保存图片

        JYSystemAuthorityModel.checkAlbunAuthority { [weak self] (res) in
            if res {
                if let image = self?.getBgImage() {
                    self?.view.HiddenHud()
                    self?.saveImage(image: image)
                }else {
                    self?.view.showErrInfo(at: "获取图片失败")
                }
            }else {
                self?.creatAlert()
            }
        }

//获取背景图片

    /// 获取背景图片
    private func getBgImage() -> UIImage? {
//这个是 全屏的图片所以加到window上
        downloadView.addcContaintView(containtView: JYWindow)
        return downloadView.jy.screenShotsImage()
    }

//添加到窗口,放到最后 布遮盖当前视图控制器, 获取截图

func addcContaintView(containtView: UIView){

containtView.addSubview(self)

self.frame = containtView.bounds

let vd:[String:UIView] = ["downloadView":self]

containtView.jy.addSubViews(vd)

containtView.addConstraints(NSLayoutConstraint.constraints(withVisualFormat: "|[downloadView]|", options: [], metrics: nil, views: vd))

containtView.addConstraints(NSLayoutConstraint.constraints(withVisualFormat: "V:|[downloadView]|", options: [], metrics: nil, views: vd))

containtView.sendSubviewToBack(self)

}

//把图片bound 改为屏幕大小

    /// 将View转成Image
    ///
    /// - Returns: UIImage
    func screenShotsImage() -> UIImage?{
        // Fallback on earlier versions
        UIGraphicsBeginImageContextWithOptions(self.base.bounds.size, false, UIScreen.main.scale)
        if let context = UIGraphicsGetCurrentContext() {
            self.base.layer.render(in: context)
           // self.base.drawHierarchy(in: self.base.bounds, afterScreenUpdates: true)
            let imamge = UIGraphicsGetImageFromCurrentImageContext()
            UIGraphicsEndImageContext()
            return imamge
        }
        return nil
    }

//保存图片

    /// 保存图片
    private func saveImage(image: UIImage) {
        UIImageWriteToSavedPhotosAlbum(image, self, #selector(self.image(image:didFinishSavingWithError:contextInfo:)), nil)
    }
    
    /// 图片保存到本地的回调
    @objc private func image(image:UIImage,didFinishSavingWithError error:NSError?,contextInfo:AnyObject) {
        
        if error != nil {
            self.view.showErrInfo(at: error.debugDescription)
        }else{
            view.showSuccessInfo(at: "图片保存成功")
            downloadView.removeFromSuperview()
        }
    }

//没开启权限的弹框

    /// 创建没有权限的弹框
    private func creatAlert() {
        let alert = UIAlertController(title: "设置权限", message: "你已关闭相册使用权限,你可以去->设置->打开设置相册权限", preferredStyle: .alert)
        alert.addAction(UIAlertAction(title: "设置", style: .default, handler: { (action) in
            if  let url = URL.init(string: UIApplication.openSettingsURLString) {
                UIApplication.shared.openURL(url)
            }
        }))
        alert.addAction(UIAlertAction(title: "取消", style: .default, handler: { (action) in
        }))

//获取当前显示的控制器
        UIViewController.getCurrentViewController()?.present(alert, animated: true, completion: nil)
    }