繁体   English   中英

iOS使用相机和UILongPressGestureRecognizer录制视频

[英]iOS record video with camera and UILongPressGestureRecognizer

我正在尝试使用UIImagePickerController ,它允许通过简单的触摸拍摄照片并将长时间的视频录制为Snapchat。

这是我的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)
    }
}

没有问题的图片部分,但对于视频工作,它只是第二次工作。 第一次触摸时,我总是遇到以下错误之一:

  • 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 UIImagePickerController: ignoring request to stop video capture; camera is not currently capturing视频

  • UIImagePickerController:忽略更改摄像头模式的请求; 相机正在拍摄视频。

你对我的问题有所了解吗?

事实上,UIImagePickerController是一个非常高级的API,因此我们必须下拉一个级别并使用AVFoundation并启动AVCaptureSession来执行此操作。 希望它有所帮助!

有关AVFoundation的快速教程可以在这里找到: http ://jamesonquave.com/blog/taking-control-of-the-iphone-camera-in-ios-8-with-swift-part-1/

由于从raywenderlich.com reidnez!

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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