使用AVCaptureSession录制视频

时间:2017-09-13 09:51:07

标签: ios iphone swift ipad avcapturesession

我正在使用AVCaptureSession中的Swift录制视频,但它工作正常,但我的客户要求是Camera移除transform animation是否有可能实现这个。

请检查我尝试过的以下代码:

var captureDevice : AVCaptureDevice!
var captureSession: AVCaptureSession?
var stillImageOutput: AVCaptureStillImageOutput?
var previewLayer: AVCaptureVideoPreviewLayer?

captureSession = AVCaptureSession()

captureSession?.sessionPreset = AVCaptureSessionPresetPhoto
if  let availbleDevices = AVCaptureDeviceDiscoverySession.init(deviceTypes: [.builtInWideAngleCamera], mediaType: AVMediaTypeVideo, position: .front).devices {
    captureDevice = availbleDevices.first    

    captureSession?.beginConfiguration()
    configureVideoPreset()
    addVideoInput()

    configureVideoOutput()
    configurePhotoOutput()
    captureSession?.commitConfiguration()

    beginSession()
    ...
}

在开始会话中,我在view.layer

中添加了预览图层
func beginSession(){        
    do {    
        let input =  try  AVCaptureDeviceInput(device: captureDevice)

        captureSession!.addInput(input)    
    } catch{
        print(error.localizedDescription)
    }
    if let previewLayer =  AVCaptureVideoPreviewLayer(session:captureSession){
        self.previewLayer = previewLayer
        self.previewLayer?.frame = view.bounds

        self.previewLayer?.videoGravity = AVLayerVideoGravityResizeAspectFill
        let dataOutput = AVCaptureVideoDataOutput()
        dataOutput.videoSettings = [kCVPixelBufferPixelFormatTypeKey as NSString:kCVPixelFormatType_32BGRA]
        dataOutput.alwaysDiscardsLateVideoFrames = true

        if captureDevice.hasFlash == true {
            do {
                try captureDevice.lockForConfiguration()
                captureDevice.flashMode = AVCaptureFlashMode.on
                captureDevice.unlockForConfiguration()
            } catch {
                print("[SwiftyCam]: \(error)")
            }
        }    

        self.view.layer.addSublayer(self.previewLayer!)
        captureSession?.commitConfiguration()

        captureSession!.startRunning()
        captureSession?.beginConfiguration()

        if (captureSession?.canAddOutput(dataOutput))!{
            captureSession?.addOutput(dataOutput)
        }
        captureSession?.commitConfiguration()

        let output = AVCaptureMetadataOutput()
        output.setMetadataObjectsDelegate(self, queue: DispatchQueue.main)
        captureSession?.addOutput(output)

        output.metadataObjectTypes = [AVMetadataObjectTypeFace]

        print("array \(output.metadataObjectTypes)")

        let queue = DispatchQueue.init(label: "com.UOO.captureQueue")
        dataOutput.setSampleBufferDelegate(self , queue: queue)
    }
}

一切正常,我也可以录制视频,但我无法用动画隐藏相机。

1 个答案:

答案 0 :(得分:0)

func setView(view: UIView, hidden: Bool) {
    UIView.transition(with: view, duration: 0.5, options: .transitionCrossDissolve, animations: {() -> Void in
        view.isHidden = hidden
    }, completion: nil)
}

使用此方法,如

  self.setView(view: previewLayer as! UIView, hidden:true)

我假设您的问题是预览图层是从视图中移除而没有动画