美文网首页
[003][iOS-Swift]使用UIImagePickerC

[003][iOS-Swift]使用UIImagePickerC

作者: 快乐捣蛋鬼 | 来源:发表于2019-06-18 13:02 被阅读0次

    流程图(完整代码见文末)

    屏幕快照 2019-06-18 下午4.07.24.png 屏幕快照 2019-06-18 下午4.37.19.png 屏幕快照 2019-06-18 下午4.38.13.png 屏幕快照 2019-06-18 下午4.38.53.png 屏幕快照 2019-06-18 下午4.44.51.png

    一.在 info.plist 中设置获取照片的权限,主要是要描述 App 获取照片权限用于做什么

    屏幕快照 2019-06-18 下午4.34.27.png
    屏幕快照 2019-06-18 下午4.31.22.png

    如果不在 info.plist 中设置,在点击 「选择照片」的按钮时会报错,见上图


    二.声明一个 ImagePickerController, 并设置其代理为当前的 VC

        
        func presentPhotoPickerController() {
            let myPickerController = UIImagePickerController()
            myPickerController.allowsEditing = true
            myPickerController.delegate = self
            myPickerController.sourceType = .photoLibrary
            self.present(myPickerController, animated: true, completion: nil)
            
        }
        
    

    设置代理 myPickerController.delegate = self 时会报错,要在class PickImageViewController 后面同时添加 UIImagePickerControllerDelegate, UINavigationControllerDelegate


    三. 实现 UIImagePickerControllerDelegate 的方法

        /* ----- UIImagePickerControllerDelegate ----- */
        func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey : Any]) {
            if let image = info[UIImagePickerController.InfoKey.editedImage] as? UIImage {
                self.UserImageView.image = image
            } else if let image = info[UIImagePickerController.InfoKey.originalImage] as? UIImage {
                self.UserImageView.image = image
            }
            dismiss(animated: true, completion: nil)
        }
        
        func imagePickerControllerDidCancel(_ picker: UIImagePickerController) {
            dismiss(animated: true, completion: nil)
        }
    

    第一个方法主要是获取用户选择的图片,并设置 UserImageView 的图片
    第二个方法主要是点击「选择图片」按钮之后跳转到图片选择界面之后不选择图片,点击取消时,dismiss。


    四.用户点击「选择图片」时触发

        @IBAction func chooseImage(_ sender: UIButton) {
            if UIImagePickerController.isSourceTypeAvailable(.photoLibrary) {
                PHPhotoLibrary.requestAuthorization { (status) in
                    switch status {
                    case .authorized: self.presentPhotoPickerController()
                    case .notDetermined:
                        if status == PHAuthorizationStatus.authorized {
                            self.presentPhotoPickerController()
                        }
                    case .denied:
                        let alert = UIAlertController(title: "没有权限获取照片信息", message: "照片权限已被拒绝,请开启权限后再更改照片", preferredStyle: .alert)
                        let confirmAction = UIAlertAction(title: "去设置", style: .default, handler: { (goSettingAction) in
                            DispatchQueue.main.async {
                                let url = URL(string: UIApplication.openSettingsURLString)!
                                UIApplication.shared.open(url, options: [:])
                            }
                        })
                        let cancelAction = UIAlertAction(title: "取消", style: .cancel)
                        alert.addAction(confirmAction)
                        alert.addAction(cancelAction)
                        self.present(alert, animated: true)
                    case .restricted: let alert = UIAlertController(title: "权限限制", message: "照片的获取被限制了无法获取", preferredStyle: .alert)
                        let okAction = UIAlertAction(title: "好的", style: .default)
                        alert.addAction(okAction)
                        self.present(alert, animated: true)
                    default: break
                    }
                }
            }
        }
    

    完整代码

    import UIKit
    import Photos
    
    class PickImageViewController: UIViewController, UIImagePickerControllerDelegate, UINavigationControllerDelegate{
    
       @IBOutlet weak var UserImageView: UIImageView!
       
       override func viewDidLoad() {
           super.viewDidLoad()
       }
       
       func presentPhotoPickerController() {
           let myPickerController = UIImagePickerController()
           myPickerController.allowsEditing = true
           myPickerController.delegate = self
           myPickerController.sourceType = .photoLibrary
           self.present(myPickerController, animated: true, completion: nil)
           
       }
       
       @IBAction func chooseImage(_ sender: UIButton) {
           if UIImagePickerController.isSourceTypeAvailable(.photoLibrary) {
               PHPhotoLibrary.requestAuthorization { (status) in
                   switch status {
                   case .authorized: self.presentPhotoPickerController()
                   case .notDetermined:
                       if status == PHAuthorizationStatus.authorized {
                           self.presentPhotoPickerController()
                       }
                   case .denied:
                       let alert = UIAlertController(title: "没有权限获取照片信息", message: "照片权限已被拒绝,请开启权限后再更改照片", preferredStyle: .alert)
                       let confirmAction = UIAlertAction(title: "去设置", style: .default, handler: { (goSettingAction) in
                           DispatchQueue.main.async {
                               let url = URL(string: UIApplication.openSettingsURLString)!
                               UIApplication.shared.open(url, options: [:])
                           }
                       })
                       let cancelAction = UIAlertAction(title: "取消", style: .cancel)
                       alert.addAction(confirmAction)
                       alert.addAction(cancelAction)
                       self.present(alert, animated: true)
                   case .restricted: let alert = UIAlertController(title: "权限限制", message: "照片的获取被限制了无法获取", preferredStyle: .alert)
                       let okAction = UIAlertAction(title: "好的", style: .default)
                       alert.addAction(okAction)
                       self.present(alert, animated: true)
                   default: break
                   }
               }
           }
       }
       
       /* ----- UIImagePickerControllerDelegate ----- */
       func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey : Any]) {
           if let image = info[UIImagePickerController.InfoKey.editedImage] as? UIImage {
               self.UserImageView.image = image
           } else if let image = info[UIImagePickerController.InfoKey.originalImage] as? UIImage {
               self.UserImageView.image = image
           }
           dismiss(animated: true, completion: nil)
       }
       
       func imagePickerControllerDidCancel(_ picker: UIImagePickerController) {
           dismiss(animated: true, completion: nil)
       }
    }
    
    
    //          ___
    //        //|||\\
    //       //|-_-|\\
    //          |||
    //           |
    //           |
    //          / \
    

    补充

    如果需要上传图像,可以压缩图片

    guard let imageData = image.jpegData(compressionQuality: 0.2) else { return }
    

    相关文章

      网友评论

          本文标题:[003][iOS-Swift]使用UIImagePickerC

          本文链接:https://www.haomeiwen.com/subject/chffqctx.html