【问题标题】:Swift 4 Image Picker Not Changing UIImageViewSwift 4 图像选择器不改变 UIImageView
【发布时间】:2019-04-12 14:52:32
【问题描述】:

由于某种原因,在我的新项目中,这段代码不起作用,之前对我有用。当前代码没有改变profileImage的ui。

代表

UIImagePickerControllerDelegate, UINavigationControllerDelegate, UIGestureRecognizerDelegate

代码:

@IBOutlet weak var profileImage: UIImageView!

@IBAction func changeProfilePicture(_ sender: Any) {
        print("Profile picture tapped")

        let pickerController = UIImagePickerController()
        pickerController.delegate = self
        pickerController.allowsEditing = true

        let alertController = UIAlertController(title: "Add Picture", message: "", preferredStyle: .actionSheet)

        let photoLibraryAction = UIAlertAction(title: "Photo Library", style: .default) { (action) in
            pickerController.sourceType = .photoLibrary
            self.present(pickerController, animated: true, completion: nil)
        }


        let cancelAction = UIAlertAction(title: "Cancel", style: .destructive, handler: nil)
        alertController.addAction(photoLibraryAction)
        alertController.addAction(cancelAction)
        present(alertController, animated: true, completion: nil)
    }

   @objc func imagePickerController(_ picker: UIImagePickerController, didFinishPickingImage image: UIImage, editingInfo: [String : Any]?) {

        self.profileImage.image = image
        self.dismiss(animated: true, completion: nil)

    }

控制台输出

发现扩展时遇到的错误:错误 Domain=PlugInKit Code=13 "查询已取消" UserInfo={NSLocalizedDescription=查询取消}

我试过了

   @objc func

    internal func

    @objc internal func

self.profileImage.image = image不设置UI和换图

【问题讨论】:

    标签: ios swift uiimageview uiimagepickercontroller


    【解决方案1】:

    正确的委托方法

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-12-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-03-30
      • 2014-11-29
      相关资源
      最近更新 更多