captureOutput function isn't called using setSampleBufferDelegate

后端 未结 6 1958
臣服心动
臣服心动 2021-02-19 03:57

I\'m starting to develop an iOS app and this is my first SO post. I\'m trying to implement a UI view which can show the preview video of the rear camera and process the captured

相关标签:
6条回答
  • 2021-02-19 04:16

    If previous answers cannot solve your problem for swift 4 and newer, correct the function name by add public can fix the problem.

    public func captureOutput(_ output: AVCaptureOutput, didOutput sampleBuffer: CMSampleBuffer, from connection: AVCaptureConnection) {}
    
    0 讨论(0)
  • 2021-02-19 04:23

    From Swift 4:

    func captureOutput(_ captureOutput: AVCaptureOutput!, 
    didOutputMetadataObjects metadataObjects: [Any]!, from connection: 
    AVCaptureConnection!)  
    

    won't be called as it no longer exists.

    It has been changed to the following :

    func metadataOutput(_ output: AVCaptureMetadataOutput, didOutput metadataObjects: [AVMetadataObject], from connection: AVCaptureConnection) 
    
    0 讨论(0)
  • 2021-02-19 04:27

    According to this tutorial you need to commit your configuration before starting to run the session.

    I also see that you have multiple points where you return false before the session can start to run. Hav you checked to see if you are exiting prematurely in one of these locations? Simply a console output, or a break point on the return statements can give you some info.

    0 讨论(0)
  • 2021-02-19 04:30

    The problem got fixed when i changed dualCamera to AVCaptureDeviceType.builtInWideAngleCamera swift 4. Hope it helps anyone in need.

    0 讨论(0)
  • 2021-02-19 04:41

    I have finally managed to find the cause of the issue. You need to make sure to use the correct function signature for the captureOutput function for the Swift 3 syntax.

    func captureOutput(_ output: AVCaptureOutput, didOutput sampleBuffer: CMSampleBuffer, from connection: AVCaptureConnection)
    

    NOT

    func captureOutput(_ output: AVCaptureOutput, didOutputSampleBuffer sampleBuffer: CMSampleBuffer, from connection: AVCaptureConnection)
    

    I was using older version of the Swift syntax and the compiler did not warn me of the issue! After correcting the function signatures, the captureOutput function gets called beautifully:-)

    0 讨论(0)
  • 2021-02-19 04:41

    I had a similar issue, however my issue was due to the way I was creating the AVCaptureSession.

    I had opted to create the AVCaptureSession in a function and return it to the caller, this part is fine, however, I neglected to hold a reference to the session in a class variable which was causing the AVCaptureSession to go out of scope and be collected by ARC before the delegate could get invoked.

    Once I set the class variable so that the AVCaptureSession wasn't released when the calling function exited everything started to work.

    Wanted to share just in case someone else ran into the same issue.

    0 讨论(0)
提交回复
热议问题