【问题标题】:UIImage not passing from one ViewController to otherUIImage 没有从一个 ViewController 传递到另一个
【发布时间】:2016-10-27 14:08:42
【问题描述】:

我正在尝试将从UIImagePickerController 捕获的图像传递给另一个ViewController,但图像没有通过。

这是第一个ViewController的代码:

func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : AnyObject])
{
    chosenImage = info[UIImagePickerControllerOriginalImage] as! UIImage 
    newImageView.contentMode = .scaleAspectFit 
    newImageView.image = chosenImage 

    self.performSegue(withIdentifier: "pass", sender: self)

    dismiss(animated:true, completion: nil) 

    let svc = self.storyboard!.instantiateViewController(withIdentifier: "demoViewController") as! demoViewController
   present(svc, animated: true, completion: nil)

}

override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
    if segue.identifier == "pass"
    {
        let imgpass = segue.destination as! demoViewController
        imgpass.newphoto = chosenImage
    }
}

第二个ViewController的代码:

override func viewDidLoad() {
    super.viewDidLoad()

    myImageView.image = newphoto
}

我浏览了很多其他类似的帖子并实现了它们,但无法通过图像。

【问题讨论】:

  • 能不能给这一行加断点,看看 newphoto myImageView.image = newphoto 的值?
  • 在你的 imagePickerController func:你为什么要做performSeguepresent?你应该只做其中之一。您不能同时转到 2 个视图控制器。

标签: ios swift uiimage uiimagepickercontroller uistoryboardsegue


【解决方案1】:

试试这个

    func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : AnyObject])
    {
        if let chosenImage = info[UIImagePickerControllerOriginalImage] as? UIImage {

        newImageView.contentMode = .scaleAspectFit
        newImageView.image = chosenImage

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

        let svc = self.storyboard!.instantiateViewController(withIdentifier: "demoViewController") as! demoViewController
        svc.newphoto = chosenImage
        self.present(svc, animated: true, completion: nil)
    }
}

【讨论】:

  • 很好,但你应该解释
  • 它给出了一个错误'使用未解析的标识符'imgpass''
【解决方案2】:

问题是你的func prepare(for:sender:) 实现永远不会被调用,所以你的newphoto 永远不会被设置。 prepare(for:sender:) 在转场之前发送。但是没有转场。您是在在代码中呈现,而不是在segue中呈现。

【讨论】:

  • 另外,您不应该在dismiss 之后立即执行present。在dismiss 的完成处理程序中执行present
  • 在使用“svc.imgpass = selectedImage”时出现错误“'demoViewController' 类型的值没有成员 'imgpass'”
  • 哦,来吧,你知道我的意思。自己做一些思考。不要让我们为你系鞋带。
  • @VaibhavArora 表示demoViewController 没有 有一个名为imgPass 的属性/成员/实例变量。确保它有一个!尽管正如马特所提到的,这是一个相当容易理解的错误......
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-07-25
  • 2014-10-04
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多