如何使用UIImagePickerController呈现ViewController

时间:2021-02-20 14:59:46

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.

我正在尝试呈现一个ImagePicker,在用户选择了图像之后,呈现一个图像编辑ViewController,用户可以在其中操作图像,然后将编辑后的图像发送回原始ViewController。

Question:

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?

是否有标准或最佳实践方法从最初的ViewControlle开始,然后在另一个ViewController之后呈现ImagePicker进行成像编辑,然后将它全部显示在初始ViewController中,同时使转换看起来无缝?

Is the suggestion in Try 3 below a good practice method or is there a better example of how to achieve this?

下面的尝试3中的建议是一个好的练习方法还是有更好的例子来说明如何实现这一点?

Try 1.

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

我最初的想法是通过以下代码以编程方式呈现ImagePicker,然后在选择图像后进行图像编辑ViewController。但这种方法似乎并不正确。在拾取图像后,我似乎无法使ImagePicker正确地切换到ViewController。 (编辑ViewController时出现了一堆“意外发现nil,同时展开可选值”错误。)

    let imagePicker = MyImagePickerController()
    imagePicker.delegate = self
    imagePicker.sourceType = .PhotoLibrary
    imagePicker.allowsEditing = false
    imagePicker.modalTransitionStyle = UIModalTransitionStyle.CoverVertical
    self.presentViewController(imagePicker, animated: true, completion: nil)

Try 2.

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.

我的第二次尝试是将图像编辑ViewController呈现为隐藏/透明,然后立即加载ImagePicker,但这会导致一些空背景闪烁。

Try 3.

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/

所以我遇到了这个建议,这是我第二次尝试的变体,使用窗口背景显示当前ViewController的图像,这似乎是可能的。 http://xissburg.com/presenting-multiple-modal-view-controllers-at-once/

Try 4.

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,它只处理一个ImagePicker,然后通过接口构建器中的segue连接每个ViewController。

1 个解决方案

#1


2  

Have you try like this, on your first ViewController show the picker on viewDidLoad with no presenting animation.

您是否尝试过这样,在您的第一个ViewController上显示viewDidLoad上的选择器而没有呈现动画。

let imagePicker = UIImagePickerController()
imagePicker.delegate = self
imagePicker.sourceType = .PhotoLibrary
imagePicker.allowsEditing = false
imagePicker.modalTransitionStyle = UIModalTransitionStyle.CoverVertical
self.presentViewController(imagePicker, animated: false, completion: nil)

Now on

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

#1


2  

Have you try like this, on your first ViewController show the picker on viewDidLoad with no presenting animation.

您是否尝试过这样,在您的第一个ViewController上显示viewDidLoad上的选择器而没有呈现动画。

let imagePicker = UIImagePickerController()
imagePicker.delegate = self
imagePicker.sourceType = .PhotoLibrary
imagePicker.allowsEditing = false
imagePicker.modalTransitionStyle = UIModalTransitionStyle.CoverVertical
self.presentViewController(imagePicker, animated: false, completion: nil)

Now on

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