问题
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) {
// The info dictionary may contain multiple representations of the image. You want to use the original.
guard let selectedImage = info[.originalImage] as?
UIImage else {
fatalError("Expected a dictionary containing an image, but was provided the following: \(info)")
}
// Set photoImageView to display the selected image.
photoImageView.image = selectedImage
// Dismiss the picker.
dismiss(animated: true, completion: nil)
}
I am trying to build an iOS app that allows you to pick an image from the library, but it keeps failing with the error "UIImage? is not convertible to UIImage. Is there a way to fix this error or bypass it as of now? I am running Xcode 10.1 on MacOS 10.14.
回答1:
Replace .originalImage
with UIImagePickerControllerOriginalImage
This worked for me.
Xcode version: 10.1
Swift version: 4.0
回答2:
info[.originalImage] is a Any? value. Si it is like it can contain a UIImage? value or nil. So, try:
guard guard let selectedImage = info[.originalImage] as? UIImage? else { ... }
adding that question mark to UIImage. That's all.
来源:https://stackoverflow.com/questions/53137795/uimage-isnt-convertible-to-uiimage-xcode-throws-error-when-trying-to-use-orig