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

Posted

技术标签:

【中文标题】错误:无法使用“UIImagePickerController.InfoKey”类型的索引为“[String:Any]”类型的值下标 [重复]【英文标题】:error: Cannot subscript a value of type '[String : Any]' with an index of type 'UIImagePickerController.InfoKey' [duplicate] 【发布时间】:2018-12-29 19:31:52 【问题描述】:

我正在尝试重建 Apple 测试应用程序以通过 CoreML 进行图像检测,但出现错误:

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

extension ImageClassificationViewController: UIImagePickerControllerDelegate, UINavigationControllerDelegate 
    func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String: Any]) 
        picker.dismiss(animated: true)

        let image = info[UIImagePickerControllerOriginalImage] as! UIImage
        imageView.image = image
        updateClassifications(for: image)
    

错误出现在一行中:

let image = info[UIImagePickerControllerOriginalImage] as! UIImage

【问题讨论】:

【参考方案1】:

看来您使用的是 Xcode 10/ios 12。委托方法的签名已更改。

使用UIImagePickerController.InfoKey 而不是String 作为info 键。

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

然后使用.originalImage 代替UIImagePickerControllerOriginalImage

extension ImageClassificationViewController: UIImagePickerControllerDelegate, UINavigationControllerDelegate 
    func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey: Any]) 
        picker.dismiss(animated: true)

        let image = info[.originalImage] as! UIImage
        imageView.image = image
        updateClassifications(for: image)
    

【讨论】:

以上是关于错误:无法使用“UIImagePickerController.InfoKey”类型的索引为“[String:Any]”类型的值下标 [重复]的主要内容,如果未能解决你的问题,请参考以下文章

SwiftUI 全屏 UIImagePickerController(相机)

裁剪和旋转 UIImagePickerController 选择的照片

iOS 图片上传处理 图片压缩 图片处理

为啥此裁剪代码会导致图像旋转?

iOS 8 UIImagePickerController 在横向模式下具有黑色预览

如何将UIImagePicker图像转换为字节数组objective-c