How to present a ViewController with a UIImagePick

2019-04-29 14:47发布

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.

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?

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

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

    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.

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/

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.

1条回答
我命由我不由天
2楼-- · 2019-04-29 15:30

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)

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.
    })
}
查看更多
登录 后发表回答