简体   繁体   中英

Swift: Segue directly to a view controller from camera/UIImagePickerController

In my app, I want the user to be able to be able to take a picture, be presented with the picture, and by tapping the photo a textfield can be added so that they can write on top of the image. This is exactly the same as the functionality of adding text to pictures in Snapchat.

As far as I can understand, the only way to be presented the image after having taken it and be able to edit it, is to set:

       imagePicker.showsCameraControls = false

Make a custom overlay:

@IBAction func takePhoto(sender: UIButton) {
    imagePicker =  UIImagePickerController()
    imagePicker.delegate = self
    imagePicker.sourceType = .Camera
    imagePicker.showsCameraControls = false
    imagePicker.allowsEditing = true

    let overlayView = UIView(frame: CGRectMake(0, self.view.frame.width, self.view.frame.width, self.view.frame.height-self.view.frame.width))
    overlayView.backgroundColor = UIColor.blackColor()
    overlayView.alpha = 0.5
    println(overlayView)

    let snapButton = UIView(frame: CGRectMake(150, 160, 80, 80))
    snapButton.layer.cornerRadius = 40
    snapButton.userInteractionEnabled = true
    snapButton.backgroundColor = UIColor.purpleColor()
    overlayView.addSubview(snapButton)
    overlayView.bringSubviewToFront(snapButton)
    let recognizer = UITapGestureRecognizer(target: self, action:Selector("handleSnapTap:"))
    recognizer.delegate = self
    snapButton.addGestureRecognizer(recognizer)

    let cancelButton = UIView(frame: CGRectMake(40, 40, 44, 44))
    cancelButton.layer.cornerRadius = 22
    cancelButton.userInteractionEnabled = true
    cancelButton.backgroundColor = UIColor.redColor()
    overlayView.addSubview(cancelButton)
    overlayView.bringSubviewToFront(cancelButton)
    let cancelRecognizer = UITapGestureRecognizer(target: self, action:Selector("handleCancelTap:"))
    cancelRecognizer.delegate = self
    cancelButton.addGestureRecognizer(cancelRecognizer)


    let changeCameraButton = UIView(frame: CGRectMake(165, 40, 44, 44))
    changeCameraButton.layer.cornerRadius = 22
    changeCameraButton.userInteractionEnabled = true
    changeCameraButton.backgroundColor = UIColor.blueColor()
    overlayView.addSubview(changeCameraButton)
    overlayView.bringSubviewToFront(changeCameraButton)
    let changeCameraRecognizer = UITapGestureRecognizer(target: self, action:Selector("handleChangeCameraTap:"))
    changeCameraRecognizer.delegate = self
    changeCameraButton.addGestureRecognizer(changeCameraRecognizer)

    let flashButton = UIView(frame: CGRectMake(300, 40, 44, 44))
    flashButton.layer.cornerRadius = 22
    flashButton.userInteractionEnabled = true
    flashButton.backgroundColor = UIColor.yellowColor()
    overlayView.addSubview(flashButton)
    overlayView.bringSubviewToFront(flashButton)
    let flashRecognizer = UITapGestureRecognizer(target: self, action:Selector("handleFlashTap:"))
    flashRecognizer.delegate = self
    flashButton.addGestureRecognizer(flashRecognizer)

    imagePicker.cameraOverlayView = overlayView

    presentViewController(imagePicker, animated: true, completion: nil)
}

func handleSnapTap(recognizer: UITapGestureRecognizer) {
   println("Take picture")
    imagePicker.takePicture()
    self.performSegueWithIdentifier("cameraToImageViewSegue", sender: self)

}

func handleCancelTap(recognizer: UITapGestureRecognizer) {
    println("Cancel")
     self.imagePicker.dismissViewControllerAnimated(true, completion: nil)
}

func handleChangeCameraTap(recognizer: UITapGestureRecognizer) {

    if (hasChangedCamera == nil){
   imagePicker.cameraDevice = UIImagePickerControllerCameraDevice.Front
        hasChangedCamera = true
        return
    }

    if (hasChangedCamera == true){
        imagePicker.cameraDevice = UIImagePickerControllerCameraDevice.Rear
        hasChangedCamera = false
        return
    }

    if (hasChangedCamera! == false){
        imagePicker.cameraDevice = UIImagePickerControllerCameraDevice.Front
        hasChangedCamera = true
        return

    }
}

func handleFlashTap(recognizer: UITapGestureRecognizer) {

    if (hasTurnedOnFlash == nil){
        imagePicker.cameraFlashMode = UIImagePickerControllerCameraFlashMode.On
        hasTurnedOnFlash = true
        return
    }

    if (hasTurnedOnFlash == true){
        imagePicker.cameraFlashMode = UIImagePickerControllerCameraFlashMode.Off
        hasTurnedOnFlash = false
        return
    }

    if (hasTurnedOnFlash == false){
        imagePicker.cameraFlashMode = UIImagePickerControllerCameraFlashMode.On
        hasTurnedOnFlash = true
        return
    }

}

And finally present a new view controller in which the picked image is placed in a UIView, and edit it from there. My issue is how to segue directly from the UIImagePickerController to a new view controller. I have tried the following:

func imagePickerController(picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [NSObject : AnyObject]) {

    self.imagePicker.dismissViewControllerAnimated(false, completion: nil)
   let vc = ModifyCameraImageViewController() //change this to your class name
    self.presentViewController(vc, animated: false, completion: nil)

}

First off, this just leads to a black screen, but I'm sure there's a simple enough way around that. My main issue is the fact that the view controller from which the UIImagePickerController was presented briefly appears on the screen before the next view controller appears. This obviously does not look good. I also tried removing the dismissViewController function, as well as placing the presentViewController function above the dismissView controller function. Both of these attempts gave me the error message:

Warning: Attempt to present <xxx.ModifyCameraImageViewController: 0x145e3eb70> on <xxx.ViewController: 0x145d20a60> whose view is not in the window hierarchy!

Attempting to use performSegueWithIdentifier with a segue linking the underlying view and the next view controller gives the same error warning.

I have found the following similar question, but I am completely inept at Objective C, so I'm struggling to make any sense of it: Push a viewController from the UIImagePickerController camera view

So, can anyone help in regards to how to present a view controller directly from the UIImagePickerController?

Also, keep in mind that I'm doing this in order to be able to create a text overlay on the newly picked image (like in Snapchat), so if anyone has a more elegant solution to that, feel free to post it!

Thanks!

Ok, found a simple solution to my issue. Instead of presenting the imagePickerController from the underlying view controller when the takePicture button is pressed, and segueing to another view controller directly from there, use the takePicture button to segue to another view controller and present the imagePickerController from the viewDidLoad of the second view controller. The second view controller will then be presented when the imagePickerController is dismissed. This however requires the underlying view controller to look similar to the camera controls, and some playing around with animations for this to look natural.

let pickerController: Void = picker.dismissViewControllerAnimated(true) { _ in
        UIImageView.image = info[UIImagePickerControllerOriginalImage] as? UIImage
        self.performSegueWithIdentifier("segueIdentifier", sender: nil)
        //Do what you want when picker is dismissed
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM