简体   繁体   English

使用与拍照相同的方法快速录制视频

[英]Swift record video using same method as taking photo

I'm currently using the following code to allow users to take photos: 我目前正在使用以下代码来允许用户拍照:

private func configurePhotoView() {
    capturedPhoto.contentMode = .ScaleAspectFill
    capturedPhoto.clipsToBounds = true
    capturedPhoto.hidden = true

    captureSession = AVCaptureSession()
    captureSession!.sessionPreset = AVCaptureSessionPresetPhoto

    photoCaptureDevice = AVCaptureDevice.defaultDeviceWithMediaType(AVMediaTypeVideo)

    var error: NSError?
    photoDeviceInput = AVCaptureDeviceInput(device: photoCaptureDevice, error: &error)

    if error == nil && captureSession!.canAddInput(photoDeviceInput) {
        captureSession!.addInput(photoDeviceInput)

        stillImageOutput = AVCaptureStillImageOutput()
        stillImageOutput!.outputSettings = [AVVideoCodecKey: AVVideoCodecJPEG]
        if captureSession!.canAddOutput(stillImageOutput) {
            captureSession!.addOutput(stillImageOutput)

            previewLayer = AVCaptureVideoPreviewLayer(session: captureSession)
            previewLayer!.videoGravity = AVLayerVideoGravityResizeAspectFill
            previewLayer!.connection?.videoOrientation = AVCaptureVideoOrientation.Portrait
            cameraView.layer.addSublayer(previewLayer)

            captureSession!.startRunning()

            self.view.addGestureRecognizer(UITapGestureRecognizer(target: self, action: "focusPhoto:"))
        }
    }
    else {
        //TODO: Handle error
    }

    photoOverlay.backgroundColor = UIColor(white: 0, alpha: 0.5)
}

When they press a button this function gets called: 当他们按下按钮时,此函数将被调用:

@IBAction func didPressTakePhoto(sender: UIButton) {
    if let videoConnection = stillImageOutput!.connectionWithMediaType(AVMediaTypeVideo) {
        videoConnection.videoOrientation = AVCaptureVideoOrientation.Portrait
        stillImageOutput?.captureStillImageAsynchronouslyFromConnection(videoConnection, completionHandler: {(sampleBuffer, error) in
            if (sampleBuffer != nil) {
                var imageData = AVCaptureStillImageOutput.jpegStillImageNSDataRepresentation(sampleBuffer)
                var dataProvider = CGDataProviderCreateWithCFData(imageData)
                var cgImageRef = CGImageCreateWithJPEGDataProvider(dataProvider, nil, true, kCGRenderingIntentDefault)

                self.capturedImage = UIImage(CGImage: cgImageRef, scale: 1.0, orientation: UIImageOrientation.Right)

                self.capturedPhoto.image = self.capturedImage
            }
        })
    }
}

This code lets me take photos with whatever aspect ratio I desire. 这段代码可以让我以所需的纵横比拍照。 Is there a way I can modify the didPressTakePhoto code to make a video? 有什么方法可以修改didPressTakePhoto代码以制作视频吗?

I can't find even one swift tutorial on how to make a custom video recorder. 我什至找不到关于如何制作自定义录像机的快速教程。

Your AVCaptureSession currently only has one output, which is an AVCaptureStillImageOutput . 您的AVCaptureSession当前只有一个输出,即AVCaptureStillImageOutput If you want to capture video, you need to add either an AVCaptureVideoDataOutput or an AVCaptureMovieFileOutput as an output to your capture session. 如果要捕获视频,则需要将AVCaptureVideoDataOutputAVCaptureMovieFileOutput为捕获会话的输出。

AV Foundation Programming Guide here: Still and Video Media Capture . AV Foundation编程指南: 静止和视频媒体捕获

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

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