首页 > 解决方案 > 限制 AVCaptureVideoDataOutputSampleBufferDelegate 调用

问题描述

有谁知道如何限制正在调用的 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)
}

标签: iosswift4

解决方案


想不通....最后,我实现了一个计时器,并且仅在计时器经过足够多的时间后才调用委托....


推荐阅读