13

试图将聊天客户端从 swift 4 传递到 swift 4.2 并使用选择器我发现了麻烦。

UIImagePickerControllerEditedImage 无法使用“UIImagePickerController.InfoKey”类型的索引为“[String : Any]”类型的值下标

func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) {

    if let pickedImage = info[UIImagePickerControllerEditedImage] as? UIImage {
        self.userProfileImage.contentMode = .scaleAspectFit
        self.userProfileImage.image = pickedImage
    }

    picker.dismiss(animated: true, completion: nil)
}
4

6 回答 6

22

方法签名已更改为

func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey : Any])

您应该会看到 func 名称的警告消息

实例方法 'imagePickerController( :didFinishPickingMediaWithInfo:)' 几乎匹配协议 'UIImagePickerControllerDelegate' 的可选要求 'imagePickerController( :didFinishPickingMediaWithInfo:)'

候选人的类型不匹配'(UIImagePickerController,[String:Any])->()'

将 'imagePickerController(_:didFinishPickingMediaWithInfo:)' 移动到另一个扩展以消除此警告

将 'imagePickerController(_:didFinishPickingMediaWithInfo:)' 设为私有以消除此警告

要求 'imagePickerController(_:didFinishPickingMediaWithInfo:)' 在这里声明(UIKit.UIImagePickerControllerDelegate)

于 2018-08-16T08:53:14.820 回答
9

在 Swift 4.2 imagePickerController 中,委托方法发生了一些变化。请查看以下代码。

func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey : Any]) {

    var selectedImage: UIImage?
    if let editedImage = info[.editedImage] as? UIImage {
        selectedImage = editedImage
        self.profileImage.image = selectedImage!
        picker.dismiss(animated: true, completion: nil)
    } else if let originalImage = info[.originalImage] as? UIImage {
        selectedImage = originalImage
        self.profileImage.image = selectedImage!
        picker.dismiss(animated: true, completion: nil)
    }

}

这肯定会奏效。

于 2018-11-01T13:17:49.947 回答
7

像这样

func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey: Any]) {
    picker.dismiss(animated: true, completion: nil)
    let image = info[UIImagePickerController.InfoKey.originalImage]! as! UIImage
    ...
}
于 2018-10-09T13:09:46.103 回答
3

斯威夫特4.2

 //MARK:- ImagePicker Controller Delegate
 //MARK:-

func imagePickerControllerDidCancel(_ picker: UIImagePickerController) {
    dismiss(animated: true, completion: nil)
}

func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey : Any]) {
    if let chosenImage = info[.originalImage] as? UIImage {
        yourimage.contentMode = .scaleAspectFill
        yourimage.image = chosenImage

    } else{
        print("Something went wrong")
    }
}
于 2019-03-09T06:18:54.903 回答
2
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey : Any]) {

        if let pickedImage = info[UIImagePickerController.InfoKey.editedImage] as? UIImage {
            yourImage.image = pickedImage
        }else{
            print("Something went wrong!!")
        }
    }
于 2019-04-16T09:06:15.607 回答
0
extension CameraViewController: UIImagePickerControllerDelegate, UINavigationControllerDelegate {

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

上面的代码解决了警告和图片成功上传

于 2019-04-09T03:16:18.560 回答