节流AVCaptureVideoDataOutputSampleBufferDelegate调用

时间:2018-05-16 12:01:46

标签: ios swift4

有谁知道如何限制调用sampleBufferDelegate调用? 我希望每秒只能调用15次,但不知道如何处理这个......

private lazy var captureSession: AVCaptureSession = {
    let fps: Int32 = 15
    let session = AVCaptureSession()
    session.sessionPreset = AVCaptureSession.Preset.photo
    guard
        let backCamera = AVCaptureDevice.default(.builtInWideAngleCamera, for: .video, position: .back),
        let input = try? AVCaptureDeviceInput(device: backCamera)
        else { return session }

    do {
        try backCamera.lockForConfiguration()
        backCamera.activeVideoMinFrameDuration = CMTimeMake(1, fps)
        backCamera.activeVideoMaxFrameDuration = CMTimeMake(1, fps)
        backCamera.unlockForConfiguration()
    } catch {
        Logger.error(error.localizedDescription)
    }

    session.addInput(input)
    return session
}()

public lazy var cameraLayer = AVCaptureVideoPreviewLayer(session: self.captureSession)

//--------------------------------------------------------------------------
// MARK: - View life cycle
//--------------------------------------------------------------------------

override func awakeFromNib() {
    super.awakeFromNib()

    self.cameraLayer.connection?.videoOrientation = self.transformOrientation(orientation: UIInterfaceOrientation(rawValue: UIApplication.shared.statusBarOrientation.rawValue)!)
    self.layer.insertSublayer(self.cameraLayer, at: 0)
    self.cameraLayer.videoGravity = .resizeAspectFill

    let videoOutput = AVCaptureVideoDataOutput()
    videoOutput.videoSettings = [kCVPixelBufferPixelFormatTypeKey as String: Int(kCVPixelFormatType_32BGRA)]
    videoOutput.setSampleBufferDelegate(self, queue: DispatchQueue(label: "MyQueue"))
    self.captureSession.addOutput(videoOutput)
    self.captureSession.startRunning()

    let tapGestureRecognizer = UITapGestureRecognizer(target: self, action: #selector(getManual(sender:)))
    tapGestureRecognizer.cancelsTouchesInView = false
    self.addGestureRecognizer(tapGestureRecognizer)

    self.addSubViewToBoundaries(self.detectedImagesView, edgeInsets: .zero)
    self.detectedImagesView.isUserInteractionEnabled = false
    self.insertSubview(self.detectedImagesView, at: 1)
}

1 个答案:

答案 0 :(得分:0)

无法搞清楚....最后,我实现了一个计时器,只有在计时器已经足够时才调用委托....