拾取图像后,imageview不会更新

时间:2017-02-08 17:04:46

标签: ios swift uiimageview swift3

我有以下问题。我使用imagepickerdelegate从我的照片库中选择一个图像并在UIImageView中应用它。问题是它不会用我选择的图像更新UIImageview。

我继承了

  

UIImagePickerControllerDelegate,UINavigationControllerDelegate

这是我的图片视图。

let profileImageView: UIImageView = {

    let imageView = UIImageView()
    imageView.translatesAutoresizingMaskIntoConstraints = false
    imageView.layer.cornerRadius = 99  / 2
    imageView.layer.masksToBounds = true
    imageView.image = UIImage(named: "profileimg")
    imageView.contentMode = .scaleToFill
    return imageView

}()

然后处理在UIImageView中选择和设置图像的函数如下所示。

func addProfilePhoto() {

    let imagePicker = UIImagePickerController()
    imagePicker.delegate = self
    imagePicker.allowsEditing = true
    imagePicker.sourceType = .photoLibrary
    imagePicker.mediaTypes = UIImagePickerController.availableMediaTypes(for: .photoLibrary)!

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

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

    var selectedImageFromPicker: UIImage?

    if let editedPickedImage = info[UIImagePickerControllerEditedImage] as? UIImage {
        selectedImageFromPicker = editedPickedImage
    } else if let pickedImage = info[UIImagePickerControllerOriginalImage] as? UIImage {
        selectedImageFromPicker = pickedImage
    }

    if let selectedImage = selectedImageFromPicker {
        print("set image")
        DispatchQueue.global(qos: .userInitiated).async {
            DispatchQueue.main.async {
                self.profileImageView.image = selectedImage
                self.dismiss(animated: true, completion: nil)
            }
        }
    }
}

func imagePickerControllerDidCancel(_ picker: UIImagePickerController) {
    dismiss(animated: true, completion: nil)
}

当我点击UIImageView时,我的日志会显示以下消息:

  

[Generic]创建具有未知类型的图像格式是错误

当我选择一张图片时,它确认有一张图片,但它不会显示。

  

设置图片

知道我做错了吗?

1 个答案:

答案 0 :(得分:0)

我检查了你的代码,似乎你没有在屏幕上显示profileImageView的代码。所以,你没有看到它。示例:

profileImageView.frame = ........
self.view.addSubview(profileImageView)