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

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM