ios - 如何从 UIImagePicker 推送到 UIViewController

标签 ios swift

一旦用户完成选择图像,我无法从图像选择器弹出到 View Controller 。基本上我让用户拍照,然后将 View Controller 弹出到一个新的 View Controller ,该 View Controller 位于杂色 Controller 中。这是我现在的代码,它不起作用。我也不想回到我打开选择器的 View Controller ,因为我想去一个编辑区域(单独的 View )。

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)
    })
}

请不吝赐教,谢谢。

最佳答案

为什么不这样做:

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

这将关闭 imagePickerController 并将您带回到您从中打开 imagePickerController 的 Controller 。

更新:
因此,首先创建一个从包含 imagePicker 的 viewController 到您希望在用户完成选择图像时将图像传递到的新 viewController 的 segue。

之后你只需调用segue。 segue 标识符现在是 ShowNewVC 将其命名为适当的名称,在 prepare 函数中,您只需获取目标 viewController 并传递图像,因此您需要声明一个图像变量您可以设置。

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
        }
    }
}

关于ios - 如何从 UIImagePicker 推送到 UIViewController,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46740181/

相关文章:

swift - 菜单栏应用程序中的 OS X 键事件 (Swift)

ios - Stripe 布局 ios 键盘默认隐藏。

ios - 只有在第一个功能完成后才执行一个功能 - ios

ios - 如何从嵌套数组中的 NSmutablearray 获取对象值?

ios - 为什么 UIImage 不会改变?

特定类的 Swift 协议(protocol)

swift - 尝试使用完成处理程序从 google firebase 获取数据时遇到问题

ios - 检查对象类型失败,出现 "is not a type"错误

android - 设计 "Platform-Independent"GWT 服务器的最佳方法是什么?

ios - iphone 的 css3 大小转换