UIImageView将不会显示从UIImagePicker中选择的图像

时间:2018-12-29 15:20:26

标签: ios swift uiimageview

我的imageView无法显示我使用UIImagePicker选择的图片。

这是所有代码:

@IBAction func addImage(_ sender: Any) {
    if !didShowCamera {
        imagePicker = UIImagePickerController()
        imagePicker.delegate = self

        if UIImagePickerController.isSourceTypeAvailable(.camera) {
            imagePicker.sourceType = .camera
            imagePicker.cameraCaptureMode = .photo
        } else {
            imagePicker.sourceType = .photoLibrary
        }

        self.present(imagePicker, animated: true, completion: nil)
    }
}

func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey : Any]) {
    let image = info[UIImagePickerController.InfoKey.originalImage] as! UIImage
    print(image)
    self.jobImage.image = takenImage
    self.takenImage = image
    didShowCamera = true
    self.dismiss(animated: true, completion: nil)
}

有什么办法解决吗?

3 个答案:

答案 0 :(得分:1)

使用调度主队列

DispatchQueue.main.async {
self.jobImage.image = takenImage
self.takenImage = image
}

答案 1 :(得分:0)

我猜在分配给图像视图时takenImage为零,您需要先将图像分配给takenImage,然后再设置为图像视图。请更新类似的方法,

func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey : Any]) {
    let image = info[UIImagePickerController.InfoKey.originalImage] as! UIImage
    print(image)
    self.takenImage = image
    self.jobImage.image = takenImage
    didShowCamera = true
    self.dismiss(animated: true, completion: nil)
}

答案 2 :(得分:0)

替换

['a', 'e', 'i', 'o', 'u']

map

并检查是否在info.plist中添加了隐私照片库访问权限。

请参阅[https://developer.apple.com/library/archive/referencelibrary/GettingStarted/DevelopiOSAppsSwift/WorkWithViewControllers.html#//apple_ref/doc/uid/TP40015214-CH6-SW1][1]

中的详细信息