QTCaptureOutput.delegate captureOutput:didOutputVideoFrame:...从未调用过

时间:2012-02-01 18:30:26

标签: objective-c cocoa delegates callback qtkit

来源

所以,我设置了QTCaptureSession

    //Setup Camera
    cameraSession = [[QTCaptureSession alloc] init];
    QTCaptureDevice *camera = [QTCaptureDevice deviceWithUniqueID: cameraID];

    BOOL success = [camera open: &error];
    if (!success || error)
    {
        NSLog(@"Could not open device %@.", cameraID);
        NSLog(@"Error: %@", [error localizedDescription]);
        return nil;
    }

    //Setup Input Session
    QTCaptureDeviceInput *cameraInput = [[QTCaptureDeviceInput alloc] initWithDevice: camera];

    success = [cameraSession addInput: cameraInput error: &error];
    if (!success || error)
    {
        NSLog(@"Could not initialize input session.");
        NSLog(@"Error: %@", [error localizedDescription]);
        return nil;
    }

    //Setup Output
    QTCaptureDecompressedVideoOutput *cameraOutput = [[QTCaptureDecompressedVideoOutput alloc] init];
    [cameraOutput setDelegate: self];

    success = [cameraSession addOutput: cameraOutput error: &error];
    if (!success || error)
    {
        NSLog(@"Could not initialize output session.");
        NSLog(@"Error: %@", [error localizedDescription]);
        return nil;
    }

QTCaptureDecompressedVideoOutput代表captureOutput:didOutputVideoFrame:WithSampleBuffer:fromConnection:因此:

- (void)captureOutput:(QTCaptureOutput *)captureOutput didOutputVideoFrame:(CVImageBufferRef)videoFrame withSampleBuffer:(QTSampleBuffer *)sampleBuffer fromConnection:(QTCaptureConnection *)connection
{
    NSLog(@"starting convert\n");
}

然后我使用以下方式开始捕获处理:

    [cameraSession startRunning];

所有变量初始化都很好,会话开始正常,但captureOutput:didOutputVideoFrame:withSampleBuffer:fromConnection:永远不会被调用。

上下文

这是一个使用GCC编译的命令行应用程序。它与以下框架相关联:

  • 粉底
  • 可可
  • QTKit
  • QuartzCore

相关杂记

框架不太可能掉线,因为captureOutput:didDropVideoFrameWithSampleBuffer:fromConnection:也没有被调用。

1 个答案:

答案 0 :(得分:1)

所以,在Mike Ash的帮助下,我设法弄清楚我的程序是立即终止而不是等待委托回调(根据Apple的QTKit文档,可能会在一个单独的线程)。

我的解决方案是向名为BOOL的对象添加captureIsFinished属性,然后将其添加到main()函数中:

    //Wait Until Capture is Finished
    while (![snap captureIsFinished])
    {
        [[NSRunLoop currentRunLoop] runUntilDate: [NSDate dateWithTimeIntervalSinceNow: 1]];
    }

这有效地延长了应用程序的运行循环1秒钟,检查捕获是否已完成,然后再运行一秒钟。