如何从UIImagePicker推送到UIViewController

时间:2017-10-14 02:13:06

标签: ios swift

用户完成图片选择后,我无法从图像选取器弹出到视图控制器。基本上我有一个用户拍照,然后将视图控制器弹出到一个多样化控制器中的新视图控制器。这是我现在的代码,但不起作用。我也不想回到我打开选择器的视图控制器,因为我想去一个编辑区(单独的视图)。

func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) {
    if let selectedImage = info[UIImagePickerControllerOriginalImage] as? UIImage {
        // ...
    }
    dismiss(animated: true, completion: {
        let vc = UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "sendPost")
        picker.popToViewController(vc, animated: true)
    })
}

请随时给出提示,谢谢。

2 个答案:

答案 0 :(得分:1)

为什么不这样做:

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

这将关闭imagePickerController并转回您从imagePickerController打开的控制器。

<强>更新
所以首先从viewController创建一个segue,它包含imagePicker到你想要在用户选择图像时将图像传递给的新viewController。

之后你就打电话给segue。 segue标识符现在是ShowNewVC名称到适当的名称,在prepare函数中,您只需获取目标viewController并传递图像,因此您需要声明可以设置的图像变量。 / p>

dismiss(animated: true, completion: {
    if let selectedImage = info[UIImagePickerControllerOriginalImage] as? UIImage {
        self.performSegue(withIdentifier: "ShowNewVC", sender: selectedImage)
    }
})

override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
    if segue.identifier == "ShowNewVC" {
        if let image = sender as? UIImage {
            let newVC = segue.destination as! NewViewController
            newVC.image = image
        }
    }
}

答案 1 :(得分:0)

您无法pop到新的视图控制器。使用present

func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) {
    if let selectedImage = info[UIImagePickerControllerOriginalImage] as? UIImage {
        // ...
    }
    dismiss(animated: true, completion: {
        let vc = UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "sendPost")
        present(vc, animated: true, completion: nil)
    })
}