从图库或相机中选择图像后 UIImage 未显示

UIImage is not showing after selecting image from gallery or camera

我正在尝试 select 从相机或画廊获取图像,并在我的图像视图中将 selected 图像显示到屏幕上。但是没有显示。

  func imagePickerController(_ picker: UIImagePickerController,
                               didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey : Any]) {

        guard let selectedImage = info[.originalImage] as? UIImage else {
            fatalError("Expected a dictionary containing an image, but was provided the following: \(info)")
        }

        imageview.image = selectedImage

        dismiss(animated: true, completion: nil)
    }

我错过了什么?

在我的例子中,断点函数根本没有调用

我之前遇到过同样的问题,通过如下所示设置延迟图像解决了这个问题:

DispatchQueue.main.asyncAfter(deadline: .now() + 1.0, execute: {
    self.imageview.image = selectedImage
})

希望这会有所帮助。

确保已将 UIImagePickerControllerDelegate 添加到 class 并设置了 UIImagePickerController 实例委托:

class PickerViewController: UIViewController, UIImagePickerControllerDelegate {
    var pickerController = UIImagePickerController()

    override func viewDidLoad() {
        super.viewDidLoad()
        pickerController.delegate = self
    }
}

此外,最好关闭发送给此委托的 picker 参数:

func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey : Any]) {
    ...

    picker.dismiss(animated: true, completion: nil)
}

如果您的断点函数未在调用时,那么我认为您还没有在 viewDidLoad() 中设置 imagePicker 委托

override func viewDidLoad() {
     super.viewDidLoad()
     imagePicker.delegate = self   
   }