Swift 4.2 imagePickerController问题
Swift 4.2 imagePickerController issue
试图将聊天客户端从 swift 4 传递到 swift 4.2,我发现了选择器的问题。
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])
您应该会看到函数名称的警告消息
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)
像这样
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey: Any]) {
picker.dismiss(animated: true, completion: nil)
let image = info[UIImagePickerController.InfoKey.originalImage]! as! UIImage
...
}
在 Swift 4.2 imagePickerController 委托方法中发生了一些变化。请查看以下代码。
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)
}
}
这肯定有效。
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")
}
}
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)
}
}
以上代码解决了警告,图片成功上传
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!!")
}
}
试图将聊天客户端从 swift 4 传递到 swift 4.2,我发现了选择器的问题。
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])
您应该会看到函数名称的警告消息
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)
像这样
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey: Any]) {
picker.dismiss(animated: true, completion: nil)
let image = info[UIImagePickerController.InfoKey.originalImage]! as! UIImage
...
}
在 Swift 4.2 imagePickerController 委托方法中发生了一些变化。请查看以下代码。
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)
}
}
这肯定有效。
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")
}
}
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)
}
}
以上代码解决了警告,图片成功上传
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!!")
}
}