UImage? isn't convertible to UIImage: Xcode throws error when trying to use original image

亡梦爱人 提交于 2021-02-08 12:02:30

问题


 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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!