Trying to pass chat client from swift 4 to swift 4.2 and with picker i found trouble.
UIImagePickerControllerEditedImage Cannot subscript a value of
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
...
}
extension CameraViewController: UIImagePickerControllerDelegate, UINavigationControllerDelegate {
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey : Any]) {
if let chosenImage = info[.originalImage] as? UIImage {
selectedImage = chosenImage
photo.image = chosenImage
}
dismiss(animated: true, completion: nil)
}
}
Above code solved the warning and image successfully uploaded
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)
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")
}
}
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.
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!!")
}
}