【问题标题】:didFinishPickingMediaWithInfo not being called for UIImagePickerControllerDelegate没有为 UIImagePickerControllerDelegate 调用 didFinishPickingMediaWithInfo
【发布时间】:2021-01-27 06:34:48
【问题描述】:

我正在尝试向我的 iOS 应用程序添加图片上传功能。我有一个函数 selectPicture(),当用户单击上传按钮时会调用该函数,然后会弹出一个警报,允许用户在相机和库之间进行选择。

func selectPicture() {
    let imagePicker = UIImagePickerController()
    imagePicker.delegate = self
    let alertController = UIAlertController(title: "Image Source", message: "Please choose your image source", preferredStyle: UIAlertControllerStyle.alert)
    let camera_action = UIAlertAction(title: "Camera", style: UIAlertActionStyle.default) {
        (_: UIAlertAction) -> Void in
        if UIImagePickerController.isSourceTypeAvailable(UIImagePickerControllerSourceType.camera) {
            print ("Camera Selected")
            imagePicker.sourceType = UIImagePickerControllerSourceType.camera
            imagePicker.allowsEditing = true
            imagePicker.modalTransitionStyle = UIModalTransitionStyle.coverVertical
            self.present(imagePicker, animated: true, completion: nil)
        }
    }
    let photoLibrary_action = UIAlertAction(title: "Photo Library", style: UIAlertActionStyle.default) {
        (_: UIAlertAction) -> Void in
        if UIImagePickerController.isSourceTypeAvailable(UIImagePickerControllerSourceType.photoLibrary) {
            print ("Photo Library Selected")
            
            imagePicker.sourceType = UIImagePickerControllerSourceType.photoLibrary
            imagePicker.allowsEditing = true
            imagePicker.navigationBar.titleTextAttributes = self.navbarAttributes
            imagePicker.navigationBar.tintColor = UIColor.white
            imagePicker.navigationBar.isTranslucent = false
            imagePicker.navigationBar.barTintColor = PING_ORANGE
            imagePicker.navigationController?.title = "Pick Image"
            imagePicker.modalTransitionStyle = UIModalTransitionStyle.coverVertical
            self.present(imagePicker, animated: true, completion: nil)
        }
    }
    let cancel_action = UIAlertAction(title: "Cancel", style: UIAlertActionStyle.default) {
        (_: UIAlertAction) -> Void in
        print ("Cancel Selected")
    }
    alertController.addAction(camera_action)
    alertController.addAction(photoLibrary_action)
    alertController.addAction(cancel_action)
    self.present(alertController, animated: true, completion: nil)
    
}

到目前为止一切正常,但是当我选择图像时,不会调用委托方法。

func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any], editingInfo: [AnyHashable: Any]!) {
    let image = info[UIImagePickerControllerOriginalImage] as? UIImage
    print("Image Print")
    print(image)
    if image != nil {
        print ("INSIDE IMAGE DID FINISH PICKING")
        dismiss(animated: false) {
            self.uploadProfileImage(image: image!)
        }
        
    } else {
        dismiss(animated: false) {
            
        }
    }
}

我是 swift 新手,所以我可能会遗漏一些东西。谁能帮我解决这个问题?

谢谢。

【问题讨论】:

    标签: ios swift xcode swift3 uiimagepickercontroller


    【解决方案1】:

    您可能正在使用旧代码。你的method signature 是错误的。应该是:

    func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey : Any]) {
        if let image = info[.originalImage] as? UIImage {
            print("Image Print")
            print(image)
            dismiss(animated: false) {
                self.uploadProfileImage(image: image)
            }
        } else {
            dismiss(animated: false)
        }
    }
    

    【讨论】:

      【解决方案2】:

      我使用以下方法解决了这个问题:

      func imagePickerController(_ picker: UIImagePickerController,
                                 didFinishPickingMediaWithInfo info: [String : Any]) {
          if let image = info[UIImagePickerControllerOriginalImage] as? UIImage {
              dismiss(animated: false) {
                  self.uploadProfileImage(image: image)
         
              }
          }
          else {
              dismiss(animated: false) {
              }
          }
      }
      

      【讨论】:

      • 您的 Swift 和 Xcode 版本是什么?你应该更新你的 Xcode 到最新版本
      • 我有最新的 xcode,但是 swift 版本太旧了。这个项目被搁置了很长时间,所以它只是一个临时应用程序。我正在做一个关于 react native 的新项目。
      猜你喜欢
      • 1970-01-01
      • 2018-09-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-05-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多