微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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

 

/**
 *  系统权限的获取
 */
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)


    }

 

 

//保存图片

    /// 保存图片
    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)
    }

 

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐