如何使用UIImagePickerController呈现ViewController [英] How to present a ViewController with a UIImagePickerController
问题描述
我正在尝试提供 ImagePicker
,并在用户选择了图像后,显示图像编辑 ViewController
用户可以操作图像,然后将编辑后的图像发送回原始 ViewController
。
I'm attempting to present an ImagePicker
, and there after the user has selected an image, present an image editing ViewController
where the user can manipulate the image and then send the edited image back to the original ViewController
.
问题:
是否有标准或最佳实践方法,从最初的ViewControlle开始,然后在那里展示ImagePicker在另一个ViewController之后进行成像编辑,然后将它全部显示在最初的ViewController中,同时使转换
看起来无缝?
Is there a standard or best practice approach starting with an initial ViewControlle, then present an ImagePicker there after another ViewController to do the imaging editing, and then display it all back in the initial ViewController, all while making the transitions look seamless?
是建议吗?在下面的尝试3中,一个好的练习方法还是有更好的例子来说明如何实现这个目标?
Is the suggestion in Try 3 below a good practice method or is there a better example of how to achieve this?
尝试1.
我最初的想法是通过以下代码以编程方式显示 ImagePicker
,然后是图像编辑 ViewC选择图像后,ontroller
。但这种方法似乎并不正确。在挑选图像后,我似乎无法将 ImagePicker
正确地转换为 ViewController
。 (在编辑 ViewController
时,一堆意外发现nil,同时展开可选值错误。)
My initial thought was to present the ImagePicker
programmatically via code below, followed by the image editing ViewController
after the image is selected. But that approach didn’t seem right. I couldn’t seem to get the ImagePicker
to segue properly to the ViewController
after an image was picked. (A bunch of "Unexpectedly found nil while unwrapping optional value" errors were showing for the editing ViewController
.)
let imagePicker = MyImagePickerController()
imagePicker.delegate = self
imagePicker.sourceType = .PhotoLibrary
imagePicker.allowsEditing = false
imagePicker.modalTransitionStyle = UIModalTransitionStyle.CoverVertical
self.presentViewController(imagePicker, animated: true, completion: nil)
尝试2。
我的第二次尝试是呈现图像编辑 ViewController
作为隐藏/透明,然后立即加载 ImagePicker
,但这会导致一些空背景闪烁。
My second attempt was to present an image editing ViewController
as a hidden/transparent followed immediately by the ImagePicker
loaded , but that causes some flashes of an empty background.
尝试3。
所以我遇到了这个建议,这是我第二次尝试使用窗口背景的变体显示当前 ViewControll的图像呃
这似乎是可能的。 http://xissburg.com/presenting-multiple-modal-view-控制器一次性/
So I came across this suggestion which is a variation of my second try using the window background to show an image of the current ViewController
which seems possible. http://xissburg.com/presenting-multiple-modal-view-controllers-at-once/
尝试4。
我的另一个想法是创建一个单独的 ViewController
,只处理 ImagePicker
,然后连接每个 ViewController
通过界面构建器中的segues。
My other idea is to create a separate ViewController
that handles just a ImagePicker
, and then connect each ViewController
via segues in the interface builder.
推荐答案
你有没有试过这样的,在你的第一个 ViewController
在 viewDidLoad
上显示选择器,不显示动画。
Have you try like this, on your first ViewController
show the picker on viewDidLoad
with no presenting animation.
let imagePicker = UIImagePickerController()
imagePicker.delegate = self
imagePicker.sourceType = .PhotoLibrary
imagePicker.allowsEditing = false
imagePicker.modalTransitionStyle = UIModalTransitionStyle.CoverVertical
self.presentViewController(imagePicker, animated: false, completion: nil)
现在开始
func imagePickerController(picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : AnyObject]) {
if let image = info[UIImagePickerControllerOriginalImage] as? UIImage {
}
dismissViewControllerAnimated(true, completion: {
//Present your custom editing controller also create `protocol/delegate` for getting editing image back.
})
}
这篇关于如何使用UIImagePickerController呈现ViewController的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!