随笔 - 400,  文章 - 0,  评论 - 7,  阅读 - 21万

 

#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)
    }
复制代码

 

posted on   懂事长qingzZ  阅读(1205)  评论(0编辑  收藏  举报
编辑推荐:
· AI与.NET技术实操系列:基于图像分类模型对图像进行分类
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
阅读排行:
· 分享一个免费、快速、无限量使用的满血 DeepSeek R1 模型,支持深度思考和联网搜索!
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· ollama系列01:轻松3步本地部署deepseek,普通电脑可用
· 25岁的心里话
· 按钮权限的设计及实现

< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5
点击右上角即可分享
微信分享提示