IOS records video from camera and UILongPressGestureRecognizer

I am trying to have UIImagePickerController

one that allows you to take a photo with a simple touch and record a long touch video like Snapchat.

This is my subclass UIImagePickerController

:

import UIKit
import MobileCoreServices

class ImagePickerController: UIImagePickerController, UIGestureRecognizerDelegate {

    var takeButton : UIButton

    override init ()
    {
        takeButton = UIButton ();
        super.init()

        self.sourceType = UIImagePickerControllerSourceType.Camera
        self.allowsEditing = false
        self.showsCameraControls = false
        self.mediaTypes = [kUTTypeMovie, kUTTypeImage]
        self.videoMaximumDuration = 10
        self.videoQuality = UIImagePickerControllerQualityType.TypeMedium;

        let screenSize: CGRect = UIScreen.mainScreen().bounds

        self.cameraOverlayView?.frame = CGRectMake(0, 0, screenSize.width, screenSize.height);


        takeButton.frame = CGRectMake(0, 0 , 100, 100)
        takeButton.center = CGPointMake(screenSize.width/2, screenSize.height - 60);
        takeButton.setImage(UIImage(named: "takeButton.png"), forState: UIControlState.Normal)
        takeButton.addTarget(self, action: "takePicture:", forControlEvents: UIControlEvents.TouchUpInside)

        if UIImagePickerController.isSourceTypeAvailable(UIImagePickerControllerSourceType.Camera)
        {
            let recognizer = UILongPressGestureRecognizer(target: self, action:Selector("holdAction:"))
            recognizer.delegate = self
            takeButton.addGestureRecognizer(recognizer)
        }

        self.cameraOverlayView?.clipsToBounds = true;
        self.cameraOverlayView?.addSubview(takeButton)
    }

    func takePicture(sender:UIButton!)
    {
        self.cameraCaptureMode = UIImagePickerControllerCameraCaptureMode.Photo;
        self.takePicture()
    }

    func holdAction(recognizer: UILongPressGestureRecognizer)
    {
        self.cameraCaptureMode = UIImagePickerControllerCameraCaptureMode.Video;
        if recognizer.state == UIGestureRecognizerState.Began
        {
            self.startVideoCapture()
            println("Video capturing...")
        }
        else if recognizer.state == UIGestureRecognizerState.Ended
        {
            self.stopVideoCapture()
            println("End recording !");
        }
    }

    required init(coder aDecoder: NSCoder) {
        takeButton = UIButton ();
        super.init(coder: aDecoder)
    }

    private override init(nibName nibNameOrNil: String!, bundle nibBundleOrNil: NSBundle!) {
        takeButton = UIButton ();
        super.init(nibName: nibNameOrNil, bundle: nibBundleOrNil)
    }
}

      

No problem with part of the image, but for video work, it only works the second time. On first touch, I always have one of the following errors:

  • Camera: ignoring _previewStarted because waiting for session to be rebuilt

  • UIImagePickerController: requested to stop video capture before recording stopped

  • UIImagePickerController: ignoring request to stop video capture; camera is not currently capturing

    video

  • UIImagePickerController: ignore the request to change the camera mode; the camera captures video.

Do you have an idea for my problem?

+3


source to share


1 answer


In fact, UIImagePickerController is a very high level API, so we need to reset the level and use AVFoundation and start AVCaptureSession . Hope it helps!

A small quick tutorial with AVFoundation can be found here: http://jamesonquave.com/blog/taking-control-of-the-iphone-camera-in-ios-8-with-swift-part-1/



Thanks to reidnez from raywenderlich.com!

+1


source







All Articles