UIImage没有从一个ViewController传递给其他

时间:2016-10-27 14:08:43

标签: ios swift uiimage uiimagepickercontroller uistoryboardsegue

我正在尝试将从foreach($_POST['product_name'] as $key => $name) { $price = $_POST['product_price'][$key]; ... do db stuff with $name/$price... } 捕获的图像传递到另一个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

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

2 个答案:

答案 0 :(得分: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)
    }
}

答案 1 :(得分:1)

问题在于,您的func prepare(for:sender:)实施永远不会被调用,因此您的newphoto永远不会被设置。 prepare(for:sender:)在segue之前发送。但没有segue。您正在代码中展示 ,而不是使用segue。