Reputation: 178
I am trying to implement an image picker. In the delegate method I am getting the following error.
or
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?
Upvotes: 3
Views: 1604
Reputation: 2649
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)
}
Upvotes: 1
Reputation: 1056
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
Upvotes: 0
Reputation: 9540
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!
Upvotes: 3