Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Swift 4.2 imagePickerController issue

Trying to pass chat client from swift 4 to swift 4.2 and with picker i found trouble.

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

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)
}
like image 712
George Heints Avatar asked Aug 15 '18 17:08

George Heints


5 Answers

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!!")
        }
    }
like image 199
SCS Avatar answered Sep 22 '22 04:09

SCS


Swift4.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")
    }
}
like image 44
Maulik Patel Avatar answered Nov 14 '22 23:11

Maulik Patel


The method signature has changed to

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

You should see a warning message for the func name

Instance method 'imagePickerController(:didFinishPickingMediaWithInfo:)' nearly matches optional requirement 'imagePickerController(:didFinishPickingMediaWithInfo:)' of protocol 'UIImagePickerControllerDelegate'

Candidate has non-matching type '(UIImagePickerController, [String : Any]) -> ()'

Move 'imagePickerController(_:didFinishPickingMediaWithInfo:)' to another extension to silence this warning

Make 'imagePickerController(_:didFinishPickingMediaWithInfo:)' private to silence this warning

Requirement 'imagePickerController(_:didFinishPickingMediaWithInfo:)' declared here (UIKit.UIImagePickerControllerDelegate)

like image 22
Ashley Mills Avatar answered Nov 15 '22 00:11

Ashley Mills


In Swift 4.2 imagePickerController delegate methods has changed a bit. Kindly check out the below code.

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

}

This will work for sure.

like image 9
Purnendu roy Avatar answered Nov 14 '22 22:11

Purnendu roy


like this

func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey: Any]) {
    picker.dismiss(animated: true, completion: nil)
    let image = info[UIImagePickerController.InfoKey.originalImage]! as! UIImage
    ...
}
like image 7
yaoning Avatar answered Nov 14 '22 23:11

yaoning