• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    公众号

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

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

 

#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)
    }

 


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
热门推荐
热门话题
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap