iPhone和AVFoundation的突发模式

时间:2012-01-03 16:41:02

标签: iphone avfoundation

我想使用AVFoundation框架实现突发模式(快速连续拍摄5次),但是遇到了困难。

for(int imgNum = 0; imgNum < nImages; imgNum++)
{
    float dT = imgNum*4.0 - (CFAbsoluteTimeGetCurrent() - startTime);
    NSLog(@"Waiting for %.02f seconds...\n",dT);
    [NSThread sleepForTimeInterval:dT];
    [self takeStill:videoConnection];
}

- takeStill:(AVCaptureConnection*)videoConnection
{
    [stillOut captureStillImageAsynchronouslyFromConnection:videoConnection completionHandler:^(CMSampleBufferRef imageSampleBuffer, NSError *error)
 {
     if(error)
         NSLog(@"%s",[[error localizedDescription] UTF8String]);
     NSData *imageData = [AVCaptureStillImageOutput jpegStillImageNSDataRepresentation:imageSampleBuffer];
     // {...} Save as a png
 }];
}

以这种方式拍摄一张图像效果很好。看起来,休眠线程会导致完成处理程序在所有nImages被采用之前永远不会触发,结果是imageSampleBuffer为NULL。处理这个问题的正确方法是什么?

1 个答案:

答案 0 :(得分:2)

尝试这种方法:

- (void)shoot:(NSNumber *)counter {
    int n = [counter intValue];
    if (n > 0) {
        [self takeStill:videoConnection];
        [self performSelector:@selector(shoot:) 
                   withObject:[NSNumber numberWithInt:n - 1] 
                   afterDelay:<# your delay #>];
    }
}

使用[self shoot:[NSNumber numberWithInt:5]]开始拍摄。

此外,您可以尝试多次拍摄而无需等待。 IIRC AVFoundation将为您排队一些静止图像请求。