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)
}
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!!")
}
}
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")
}
}
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)
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 this
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey: Any]) {
picker.dismiss(animated: true, completion: nil)
let image = info[UIImagePickerController.InfoKey.originalImage]! as! UIImage
...
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With