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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/51342028/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-31 09:45:39  来源:igfitidea点击:

Cannot subscript a value of type '[String : Any]' with an index of type 'UIImagePickerController.InfoKey'

iosswiftuiimagepickercontroller

提问by drobati

I'm using Apple's Swift iOS Tutorial. Which is throwing an error,

我正在使用Apple 的 Swift iOS 教程。这是抛出错误,

Cannot subscript a value of type '[String : Any]' with an index of type 'UIImagePickerController.InfoKey'

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

The function they defined is below.

他们定义的函数如下。

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

    // The info dictionary may contain multiple representations of the image. You want to use the original.
    guard let selectedImage = info[UIImagePickerControllerOriginalImage] as? UIImage else {
        fatalError("Expected a dictionary containing an image, but was provided the following: \(info)")
    }

    // Set photoImageView to display the selected image.
    photoImageView.image = selectedImage

    // Dismiss the picker.
    dismiss(animated: true, completion: nil)
}

I'm using Xcode Version 10.0 beta 3, which includes Swift 4.2.

我正在使用 Xcode 版本 10.0 beta 3,其中包括 Swift 4.2。

I'd like to understand how to traverse the docs to understand what might have changed or broken.

我想了解如何遍历文档以了解可能已更改或损坏的内容。

回答by vadian

The signature of the method has changed in Swift 4.2

方法的签名在 Swift 4.2 中发生了变化

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

and you have to write

你必须写

guard let selectedImage = info[.originalImage] as? UIImage else {
    fatalError("Expected a dictionary containing an image, but was provided the following: \(info)")
}

You can figure out such terminology changes yourself by reading the documentationor by commenting out the entire method, retype the first few characters and use code completion.

您可以通过阅读文档或注释掉整个方法、重新键入前几个字符并使用代码完成来自己找出这些术语的变化。

回答by edymerchk

I'm following also the same tutorial, the updated code looks like this:

我也在关注相同的教程,更新后的代码如下所示:

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

    // The info dictionary may contain multiple representations of the image. You want to use the original.
    guard let selectedImage = info[.originalImage] as? UIImage else {
        fatalError("Expected a dictionary containing an image, but was provided the following: \(info)")
    }

    // Set photoImageView to display the selected image.
    photoImageView.image = selectedImage

    // Dismiss the picker.
    dismiss(animated: true, completion: nil)
}

回答by Saranjith

Swift 5

斯威夫特 5

In latest version of swift 4 or 5 the delegate method is given below, it should work

在最新版本的 swift 4 或 5 中,下面给出了委托方法,它应该可以工作

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

And use,

并使用,

guard let selectedImage = info[.editedImage] as? UIImage else {

}



func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey : Any]) {
    guard let selectedImage = info[.editedImage] as? UIImage else {

    }
}

回答by Sureshkumar Linganathan

Use like this,

像这样使用,

guard let selectedImage = info[UIImagePickerController.InfoKey.originalImage.rawValue] as? UIImage else {
        fatalError("Expected a dictionary containing an image, but was provided the following: \(info)")
}

回答by MrG

In Swift 4 and 5 like so:

在 Swift 4 和 5 中,像这样:

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

    guard let selectedImage = info[.editedImage] as? UIImage else {
        fatalError("Expected a dictionary containing an image, but was provided the following: \(info)")
    }

    self.myImageView.image = selectedImage

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

回答by Saju KS

I am also following the tutorial. It is working after changing as below.

我也在关注教程。更改后它正在工作,如下所示。

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

    // The info dictionary may contain multiple representations of the image. You want to use the original.
    guard let selectedImage = info[.originalImage] as? UIImage
        else {
        fatalError("Expected a dictionary containing an image, but was provided the following: \(info)")
    }

    // Set photoImageView to display the selected image.
    photoImageView.image = selectedImage

    // Dismiss the picker.
    dismiss(animated: true, completion: nil)
}

I am using XCode 11 and Swift 5.

我正在使用 XCode 11 和 Swift 5。

回答by Akbar Khan

Method has been changed a little bit in Swift 4.2.

方法在 Swift 4.2 中略有改变。

First initialise these two variables:

首先初始化这两个变量:

var imagePicker = UIImagePickerController()
var pickedImageProduct = UIImage()

extension yourViewController: UIImagePickerControllerDelegate,UINavigationControllerDelegate{
//create an IBAction to access Camera
    @IBAction func accessCameraBtn(_ sender: UIButton) {
        imagePicker.delegate = self
        imagePicker.sourceType = UIImagePickerController.SourceType.camera
        self.present(imagePicker, animated: true, completion: nil)
    }
//create an IBAction to access Gallery
    @IBAction func accessGalleryBtn(_ sender: UIButton) {
        imagePicker.delegate = self
        imagePicker.sourceType = UIImagePickerController.SourceType.photoLibrary
        self.present(imagePicker, animated: true, completion: nil)
    }
//Final step put this Delegate    
    func imagePickerController(_ picker: UIImagePickerController,
                               didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey : Any]) {

        guard let selectedImage = info[.originalImage] as? UIImage else {
           Print("Error: \(info)")
        }

            pickedImageProduct = selectedImage

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

        dismiss(animated: true, completion: nil)
    }

}

回答by Prakhar Prakash Bhardwaj

In Swift 4.2 you can write the function as:

在 Swift 4.2 中,您可以将函数编写为:

    func photoLib()
    {
        //opens Photo Library, call the function in a @IBAction func
        let myPickerController = UIImagePickerController()
        myPickerController.delegate = self;
        myPickerController.sourceType = 
        UIImagePickerController.SourceType.photoLibrary
        myPickerController.allowsEditing = true
        present(myPickerController, animated: true)
    }


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

    guard let image = info[.editedImage] as? UIImage else {
        print("No image found")
        photoLabel.text = "Photo Not Found"
        return
    }
}