使用captureOutput捕获视频和显示图像:captureOutput didOutputSampleBuffer:sampleBuffer fromConnection:connection

问题描述 投票:3回答:1

我试图更好地理解AVFoundation框架以及各种Core xxxx框架,所以我决定尝试一个简单的视频捕获,看看我是否可以作为图像输出到UI。我查看了rosyWriter代码以及文档,但没有回答。所以:

我有标准的捕获会话代码来添加输入和输出。以下内容与问题相关:

//moving the buffer processing off the main queue
dispatch_queue_t bufferProcessingQueue=dispatch_queue_create("theBufferQueue", NULL);
[self.theOutput setSampleBufferDelegate:self queue:bufferProcessingQueue];
dispatch_release(bufferProcessingQueue);

然后是代表:

-(void) captureOutput:(AVCaptureOutput *)captureOutput didOutputSampleBuffer:(CMSampleBufferRef)sampleBuffer
       fromConnection:(AVCaptureConnection *)connection
    {

    CVPixelBufferRef pb = CMSampleBufferGetImageBuffer(sampleBuffer);

    CIImage *ciImage = [CIImage imageWithCVPixelBuffer:pb];
    CGImageRef ref = [self.theContext createCGImage:ciImage fromRect:ciImage.extent];

    dispatch_async(dispatch_get_main_queue(), ^{
        self.testBufferImage.image= [UIImage imageWithCGImage:ref scale:1.0 orientation:UIImageOrientationRight];
    });
}

问题:

1-我猜测,正如我上面所做的那样,我们应该始终将委托设置为在上面执行的单独队列上运行而不是主队列。正确?

2-结合使用,在委托方法中,任何处理UI的调用都必须像我一样放回主队列。正确?

3-当我运行此代码时,大约5-10秒后,我收到“收到内存警告”错误,应用程序关闭。什么可能导致这个?

ios avfoundation objective-c-blocks avcapturesession
1个回答
3
投票

1)一般是的,你应该。您可以在主队列上运行它,但这可能会导致UI响应等问题。

2)正确。

3)您正在创建一系列CGImageRefs。你在哪里发布它们?

出于性能原因,如果您需要对视频渲染进行精细控制,则应该使用OpenGL。否则,您可以使用AVCaptureVideoPreviewLayer轻松获取预览。

© www.soinside.com 2019 - 2024. All rights reserved.