显示从UIImagePickerController中选择的图像

时间:2017-07-16 17:34:23

标签: ios swift uiimagepickercontroller

应用程序打开pickerController并选择图像,但它不会显示所选的图像。我尝试在self之前添加image_sel.image = photo,但它既不起作用也不行。当我将print放在扩展内部时,它会打印并且它也会遇到断点。

lazy var image_sel: UIImageView = {
    let i = UIImageView()
    i.translatesAutoresizingMaskIntoConstraints = false
    i.image = #imageLiteral(resourceName: "noimage")

    return i
}()

@objc func imageSelect(){
    let pickerController = UIImagePickerController()
    pickerController.delegate = self

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

extension CameraPage: UIImagePickerControllerDelegate,UINavigationControllerDelegate {
    func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) {
        if let photo = info["UIImagePickerControllerOriginalImage"] as? UIImage {
            image_sel.image = photo
        }

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

2 个答案:

答案 0 :(得分:0)

  

lazy var image_sel:UIImageView

您正在使用lazy var属性,当您尝试将值分配给ImageView时,可能仍然没有对象。

尝试在另一个UIImageView上使用该图像来查看是否存在问题。

Check your view stack

答案 1 :(得分:0)

我还没有看到你展示image_sel的地方。您使用的是addSubview吗?