微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

没有为UIImagePickerControllerDelegate调用didFinishPickingMediaWithInfo

如何解决没有为UIImagePickerControllerDelegate调用didFinishPickingMediaWithInfo

我正在尝试向我的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,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,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) {
            
        }
    }
}

我是新手,所以我可能会缺少一些东西。有人可以帮我吗?

谢谢。

解决方法

您可能正在使用旧代码。您的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)
    }
}
,

我通过使用以下方法解决了此问题:

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) {
        }
    }
}

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。