HTML5   发布时间:2022-04-27  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了ios – 在Swift中,改编了AVCaptureVideoDataOutputSampleBufferDelegate,但是captureOutput永远不会被调用大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
我正试图从相机捕获视频帧并实时显示UI ImageView上的图像泛滥.
我试图为我的viewcontroller调整AVCaptureVideoDataOutputSampleBufferDelegate.
我还实现了captureOutput,但是从不调用captureOutput.

这是我的代码

import UIKit
import AVFoundation

class ViewController: UIViewController,AVCaptureVideoDataOutputSampleBufferDelegate {

    @IBOutlet weak var imageView: UIImageView!
    @IBOutlet var cameraView: UIView!

    var selectedImage :UIImage!

    let captureSession = AVCaptureSession()
    var captureDevice : AVCaptureDevice?
    var videoCaptureOutput : AVCaptureVideoDataOutput!

     override func viewDidLoad() {
        super.viewDidLoad()

        captureSession.sessionPreset = AVCaptureSessionPresetLow
        self.captureDevice = AVCaptureDevice.defaultDeviceWithMediaType(AVMediaTypeVideo)
        if(captureDevice != nil){
            beginSession()
        }
    }


    func beginSession() {

        dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH,0),{ 
            self.videoCaptureOutput = AVCaptureVideoDataOutput()
            self.videoCaptureOutput.videoSettings = [kCVPixelBufferPixelFormatTypeKey:kCVPixelFormatType_32BGRA]
            self.videoCaptureOutput.alwaysDiscardsLateVideoFrames = true

            self.captureSession.addOutput(self.videoCaptureOutput)

            var err : NSError? = nil
            self.captureSession.addInput(AVCaptureDeviceInput(device: self.captureDevice,error: &err))
            self.captureSession.sessionPreset = AVCaptureSessionPresetPhoto
            if err != nil {
                println("error: \(err?.localizedDescription)")
            }
            var previewLayer = AVCaptureVideoPreviewLayer(session: self.captureSession)
            previewLayer?.frame = self.cameraView.layer.bounds
            previewLayer?.videoGravity = AVLayerVideoGravityResizeAspectFill
            dispatch_async(dispatch_get_main_queue(),{ // 2
                // 3
                self.cameraView.layer.addSublayer(previewLayer)
                self.captureSession.startRunning()

            });
        });
    }

    func captureOutput(captureOutput: AVCaptureOutput!,didOutputSampleBuffer sampleBuffer: CMSampleBuffer!,fromConnection connection: AVCaptureConnection!) {

        println("capture delegation called")

        var imageProcessor = ImageProcessor()
        imageView.image = imageProcessor.imageFromSampleBuffer(sampleBuffer)
    }
}

正如您所看到的,我正在尝试处理图像并在imageview上显示实时捕获帧,假设我的ImageProcessor()工作正常….

任何帮助将不胜感激,谢谢.

解决方法

您的委托方法未被调用,因为您根本没有将委托设置为videoCaptureOutput.为此,您必须调用videoCaptureOutput setSampleBufferDelegate(self,queue:queue)方法,其中queue参数是队列,应在其上调用回调

大佬总结

以上是大佬教程为你收集整理的ios – 在Swift中,改编了AVCaptureVideoDataOutputSampleBufferDelegate,但是captureOutput永远不会被调用全部内容,希望文章能够帮你解决ios – 在Swift中,改编了AVCaptureVideoDataOutputSampleBufferDelegate,但是captureOutput永远不会被调用所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。
标签: