簡體   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