把uImage?不能转换为UIImage:Xcode在尝试使用原始图像时会抛出错误

问题描述 投票:-1回答:2
 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)
}

我正在尝试构建一个iOS应用程序,允许您从库中选择一个图像,但它一直失败,错误“UIImage?无法转换为UIImage。有没有办法解决此错误或绕过它现在?我在MacOS 10.14上运行Xcode 10.1。

ios swift uiimage uiimagepickercontroller
2个回答
0
投票

.originalImage替换UIImagePickerControllerOriginalImage

这对我有用。

Xcode版本:10.1

Swift版本:4.0


-1
投票

info [.originalImage]是一个Any?值。 Si它就像它可以包含一个UIImage?价值或零。所以,试试:

guard guard let selectedImage = info[.originalImage] as? UIImage?  else { ... }

将该问号添加到UIImage。就这样。

© www.soinside.com 2019 - 2024. All rights reserved.