当图像视图已经有图像时,Segue传递图像并显示

时间:2017-10-30 22:54:23

标签: ios swift uiviewcontroller uiimageview

我有3个视图控制器。第一个包含UIimageview,  第二个是customCameraViewController,第三个是previewController。应用程序从第一个控制器开始,在用户捕获照片后有一个导航到customCameraViewController的按钮,它会将其传递给previewController以决定保存或取消照片。如果用户保存照片,它将使用segue将uiimage传回并在第一个控制器中显示。

问题是uiimageView已经设置了“默认个人资料照片” 当用户打开应用程序时,该问题如何解决。它显示“默认照片”然后捕获照片并将其传递回firstViewController并显示“捕获的照片”

FirstviewController

var image:UIImage!


 override func viewDidLoad() {
        super.viewDidLoad()


if imageView.image == nil{
  print("nothing")
  imageView.image = UIImage(named: ("defaultPhoto"))
}else if imageView.image == UIImage(named: ("defaultPhoto")) && imageView.image != nil{
  print("have image")
  imageView.removeFromSuperview()
  imageView.image = image
}
}

PreviewViewController

 class PreviewViewController: UIViewController {

  var image: UIImage!

  @IBOutlet weak var photoPic: UIImageView!

  @IBAction func cancelAction(_ sender: Any) {
    dismiss(animated: true, completion: nil)
  }
  @IBAction func saveAction(_ sender: Any) {

    self.performSegue(withIdentifier: "savePicSegue", sender: nil)
  }
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
      if segue.identifier == "savePicSegue" {
        let destinationVC = segue.destination as! FirstViewController
        destinationVC.image = self.image
      }

    }

1 个答案:

答案 0 :(得分:-1)

更新您的FirstviewController viewDidLoad逻辑:

if image == nil {
    imageView.image = UIImage(named: ("defaultPhoto"))
} else {
    imageView.image = image
}