权限获取、照片视频下载

一、获取权限

1.1、获取相册权限(是否是首次获取)

需要在info.plist增加相册隐私权限:NSPhotoLibraryUsageDescription

    /// 检查相册存取权限,如有必要则向系统申请
    /// iOS14之后使用之前的获取状态请求(limited和authorized都会返回authorized)
    /// status:用户设置的权限
    /// firstDetermined:是否是首次设置权限
  class func checkAuthorizationStatus(_ handler: @escaping (_ status: PHAuthorizationStatus,_ firstDetermined: Bool) -> Void) {

        var authorizationStatus = PHPhotoLibrary.authorizationStatus()
        if #available(iOS 14, *) {
            authorizationStatus = PHPhotoLibrary.authorizationStatus(for: .readWrite)
        }
        if authorizationStatus == .notDetermined {
            if #available(iOS 14, *) {
                PHPhotoLibrary.requestAuthorization(for: .readWrite) { (status) in
                    handler(status, true)
                }
            } else {
                PHPhotoLibrary.requestAuthorization { (status) in
                    handler(status, true)
                }
            }
            
        } else {
            handler(authorizationStatus, false)
        }
    }

1.2、获取相机权限

需要在info.plist增加相机隐私权限:NSCameraUsageDescription

 func requestCamera() {
        AVCaptureDevice.requestAccess(for: .video) { (cameraGranted) in
            if cameraGranted {
                print("允许APP访问相机")
            } else {
                print("拒绝APP访问相机")
            }
        }
    }

1.3、获取麦克风权限

需要在info.plist增加麦克风隐私权限:NSMicrophoneUsageDescription

 func requestAudio(cameraAllowed: Bool) {
        AVCaptureDevice.requestAccess(for: .audio) { (audioGranted) in
            if audioGranted {
                print("允许APP访问麦克风")
            } else {
                print("拒绝APP访问麦克风")
            }
        }
    }

二、资源类型相关API

2.1、判断资源是否是iCloud

/*
     判断是否是iCloud照片
     asset:资源对象
     reseult: true为本地资源,false为iCloud资源
     */
     @objc open class func judgeAssetis(inLocalAblum asset: PHAsset?) -> Bool {
        var result = false
        
        if let pAsset = asset {
            if #available(iOS 10.0, *) {
                let resourceArray = PHAssetResource.assetResources(for: pAsset)
                if resourceArray.count > 0 {
                    if let iCloud: Bool = resourceArray.first?.value(forKey: "locallyAvailable") as? Bool, iCloud == true {
                        return true
                    }
                }
            }
            
            let option = PHImageRequestOptions()
            /// 不允许网络请求(iCloud资源获取失败)
            option.isNetworkAccessAllowed = false
            /// 设置为同步请求
            option.isSynchronous = true
            if let asset = asset {
                PHCachingImageManager.default().requestImageData(for: asset, options: option, resultHandler: { imageData, _, _, _ in
                    result = imageData != nil ? true : false
                })
            }
            return result
        }
        return true
    }

2.2、照片下载

// 下载照片原图
    @objc open class func requestImageData(for asset: PHAsset, options: PHImageRequestOptions?, resultHandler: @escaping (Data?, String?, UIImage.Orientation, [AnyHashable: Any]?) -> Void, progressHandler:@escaping (Double) -> Void) -> PHImageRequestID {
        // Prepare the options to pass when fetching the (photo, or video preview) image.
        var requestOptions = options
        if requestOptions == nil {
            requestOptions = PHImageRequestOptions()
            /// 照片质量
            requestOptions?.deliveryMode = .highQualityFormat
            /// 是否允许网络下载
            requestOptions?.isNetworkAccessAllowed = true
        }
        
        let progressHandler: PHAssetVideoProgressHandler = { (progress, error, stop, info) in
            if let b = info?[PHImageCancelledKey] as? NSNumber, b.boolValue {
                return
            }
            if error != nil {
                return
            }
            progressHandler(progress)
        }
        requestOptions?.progressHandler = progressHandler
        return PHImageManager.default().requestImageData(for: asset, options: requestOptions, resultHandler: resultHandler)
    }

2.3、视频下载

/// 获取视频资源
    /// - asset: 资源元数据
    /// - options: 下载参数
    /// - resultHandler: 返回结果
    /// - progressHandler: 下载进度
    open class func requestAVAssetAsset(forVideo asset: PHAsset, options: PHVideoRequestOptions?, resultHandler: @escaping (AVAsset?, AVAudioMix?, [AnyHashable: Any]?) -> Void, progressHandler:@escaping (Double) -> Void) -> PHImageRequestID {
        var requestOptions = options
        if requestOptions == nil {
            requestOptions = PHVideoRequestOptions()
            requestOptions?.deliveryMode = .automatic
            requestOptions?.isNetworkAccessAllowed = true
            
        }
        
        let progressHandler: PHAssetVideoProgressHandler = { (progress, error, stop, info) in
            if error != nil {
                return
            }
            progressHandler(progress)
        }
        requestOptions?.progressHandler = progressHandler
        return PHImageManager.default().requestAVAsset(forVideo: asset, options: requestOptions, resultHandler: resultHandler)
    }

相册相关的内容后续持续更新(相簿、照片集、iCloud资源下载进度等等),如果大家有什么疑问和建议欢迎一起谈论~~

你可能感兴趣的:(权限获取、照片视频下载)