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。
用.originalImage
替换UIImagePickerControllerOriginalImage
这对我有用。
Xcode版本:10.1
Swift版本:4.0
info [.originalImage]是一个Any?值。 Si它就像它可以包含一个UIImage?价值或零。所以,试试:
guard guard let selectedImage = info[.originalImage] as? UIImage? else { ... }
将该问号添加到UIImage。就这样。