3

I am trying to implement an image picker. In the delegate method I am getting the following error.

enter image description here

or

enter image description here

Here is the code, I wrote

func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey : Any]) {
    var image = info[.editedImage] as? UIImage
    if image == nil {
        image = info[.originalImage] as? UIImage
    }

    simpleImagePicker!.dismiss(animated: true)
}

I tried this using Swift 5 and it is working without error. But the issue is with Swift 4.

How to resolve this? Is there any solution for it?

rmaddy
  • 314,917
  • 42
  • 532
  • 579
MK iOS
  • 178
  • 15

3 Answers3

3

The UIImagePickerControllerDelegate signature is different for different Swift versions. You are using the method of Swift 4.2 and later which is far different from Swift 4.0.

Swift 4.0:

func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) {
    if let editedImage = info[UIImagePickerControllerEditedImage] as? UIImage {

    } else if let originalimage = info[UIImagePickerControllerOriginalImage] as? UIImage {

    }
}

Swift 4.2 & Swift 5:

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

    } else if let image = info[UIImagePickerController.InfoKey.originalImage] as? UIImage {

    } else {
        print("Something went wrong")
    }
}

So, check the proper swift version and follow the proper methods!

rmaddy
  • 314,917
  • 42
  • 532
  • 579
Sohil R. Memon
  • 9,404
  • 1
  • 31
  • 57
1

I tried this using Swift 5 and it is working without error. But the issue is with Swift 4.

First of all, if you're using Xcode 10.x I strongly suggest you to perform the Swift 5 update. Forget Swift 4 and stay updated.

Second, this is the right UIImagePickerControllerDelegate implementation on Swift 5:

func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey : Any]) {
    if let image = info[UIImagePickerController.InfoKey.originalImage] as? UIImage {
        // do stuff with your original image...

    } else if let image = info[UIImagePickerController.InfoKey.editedImage] as? UIImage {
        // do something with your edited image...
    }
    dismiss(animated: true, completion: nil)
}


Anyway, if you want to stick to Swift 4, this is the ImagePicker delegate function to use:

func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) {
        if let chosenImage = info[UIImagePickerControllerOriginalImage] as! UIImage {
            // do stuff with your image
        } else if let chosenImage = info[UIImagePickerControllerOriginalImage] as! UIImage {
            // do stuff with your image
        }
        dismiss(animated:true, completion: nil)
}
Frank Eno
  • 2,581
  • 2
  • 31
  • 54
  • This doesn't address the error. The OP already knows this works under Swift 5. The question is asking why that specific error is happening under Swift 4 (presumably Swift 4.0). – rmaddy Jul 05 '19 at 05:21
  • yes, but in programming you need to get updated, this is how this world goes so Swift 4 is gone, I just wanted to suggest him to update to Swift 5, which is the right thing to do :) – Frank Eno Jul 05 '19 at 05:23
  • 1
    While I agree the best solution is to stay current, that comment doesn't address the question being asked. The OP specifically asked why they are getting that error in Swift 4. Telling them to use Swift 5 doesn't answer that question. – rmaddy Jul 05 '19 at 05:25
0

The main problem is that there's no UIImagePickerController.InfoKey in Swift 4. The compiler could not define this type, which causes the error If you use correct signature for swift 4 with [String: Any] dictionary, the error will disappear

Tiran Ut
  • 942
  • 6
  • 9