Swift 使用相册相机简单完整的教程

2019-08-22  本文已影响0人  绍清_shao

内容

Info.plist 中添加对应的权限

参考相关链接进行添加:Info.plist添加权限

添加库依赖库

import Photos

相册、相机四种权限

官方文档

代码实现

相册

  1. 判断相机权限
    notDetermined第一次触发授权
    authorized用户已授权
    restrictedApp无相册访问权限
    denied用户明确拒绝授权
func judgeAlbumPermissions() {
        
        // 获取相册权限
        let authStatus = PHPhotoLibrary.authorizationStatus()
        
        //用户尚未授权
        if authStatus == .notDetermined {
            // 第一次触发授权 alert
            PHPhotoLibrary.requestAuthorization({ [weak self] (states) in
                // 判断用户选择了什么
                
                guard let strongSelf = self else { return }
                
                if states == .authorized {
                    strongSelf.openPhoto()
                    
                } else if states == .restricted || states == .denied {
                    // 提示没权限
                    
                }
                
            })
            
        } else if authStatus == .authorized {
            // 可以访问 去打开相册
            self.openPhoto()
            
        } else if authStatus == .restricted || authStatus == .denied {
            // App无权访问照片库 用户已明确拒绝
        }
        
    }
  1. 已授权的情况下打开相册
    func openPhoto() {
        
        if UIImagePickerController.isSourceTypeAvailable(.photoLibrary) {
            DispatchQueue.main.async {
                let picker = UIImagePickerController()
                picker.delegate = self
                picker.sourceType = .photoLibrary
                self.present(picker, animated: true)
            }
        }
        
    }
  1. ViewController实现代理UIImagePickerControllerDelegate、UINavigationControllerDelegate
extension HomeViewController: UIImagePickerControllerDelegate, UINavigationControllerDelegate  {
    
    func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey : Any]) {
        
        // 获取选择的图片
        guard let pickedImage = info[UIImagePickerController.InfoKey.originalImage]
            as? UIImage else {
                print("选择失败")
                return
        }
        picker.dismiss(animated: true) {
            self.dealwith(image: pickedImage)
        }
        
    }
}

相机

跟相册类似,此处直接贴代码不做说明。同样需要实现代理UIImagePickerControllerDelegate、UINavigationControllerDelegate

    func selectCamera() {
         // 判断相机权限
        let authStatus = AVCaptureDevice.authorizationStatus(for: .video)
        
        //用户尚未授权
        if authStatus == .notDetermined {
            // 第一次触发授权 alert
            PHPhotoLibrary.requestAuthorization({ [weak self] (states) in
                // 判断用户选择了什么
                
                guard let strongSelf = self else { return }
                
                if states == .authorized {
                    strongSelf.openCamera()
                    
                } else if states == .restricted || states == .denied {
                    // 提示没权限
                    SwiftNotice.showText("No access to Access System Camera!", autoClear: true, autoClearTime: 3)
                }
                
            })
            
        } else if authStatus == .authorized {
            // 可以访问 去打开相册
            self.openCamera()
            
        } else if authStatus == .restricted || authStatus == .denied {
            // App无权访问照片库 用户已明确拒绝
            SwiftNotice.showText("No access to Access System Camera!", autoClear: true, autoClearTime: 3)
        }
    }

// 打开相机
    func openCamera() {
        
        self.clearAllNotice()
        if UIImagePickerController.isSourceTypeAvailable(.camera) {
            DispatchQueue.main.async {
                let  cameraPicker = UIImagePickerController()
                cameraPicker.delegate = self
                cameraPicker.allowsEditing = true
                cameraPicker.sourceType = .camera
                //在需要的地方present出来
                self.present(cameraPicker, animated: true, completion: nil)
            }
            
        }

Demo

暂无,有时间再完善了!!!
有不明白或者出错的地方可以私信我!!
手把手教!

上一篇下一篇

猜你喜欢

热点阅读