web-dev-qa-db-fra.com

Impossible d'indiquer une valeur de type '[String: Any]' avec un index de type 'UIImagePickerController.InfoKey'

J'utilise Swift _ Tutoriel iOS d'Apple . Ce qui jette une erreur,

Impossible d'indiquer une valeur de type '[String: Any]' avec un index de type 'UIImagePickerController.InfoKey'

La fonction qu'ils ont définie est ci-dessous.

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

J'utilise Xcode Version 10.0 beta 3, qui inclut Swift 4.2.

J'aimerais comprendre comment parcourir la documentation pour comprendre ce qui a pu être modifié ou cassé.

39
drobati

La signature de la méthode a changé dans Swift 4.2

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

et vous devez écrire

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

Vous pouvez vous rendre compte de tels changements de terminologie en lisant documentation ou en commentant la méthode dans son ensemble, ressaisir les premiers caractères et utiliser la complétion de code.

91
vadian

Je suis aussi le même tutoriel, le code mis à jour ressemble à ceci:

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)
}
20
edymerchk

Utilisez comme ça,

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

Swift 5

Dans la dernière version de Swift 4 ou 5, la méthode déléguée est donnée ci-dessous, cela devrait fonctionner

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

Et utilise,

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

}
5
Saranjith

Dans Swift 4 et 5, comme ceci:

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)
}
4
MrG

La méthode a été légèrement modifiée dans Swift 4.2.

Initialisez d'abord ces deux 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)
    }

}
1
Akbar Khan

Dans Swift 4.2, vous pouvez écrire la fonction en tant que:

    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
    }
}
1