2012-01-03 20 views
1

我想使用AVFoundation框架實現突發模式(快速連續5次拍攝),但遇到了困難。使用iPhone和AVFoundation的突發模式

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。處理這個問題的正確方法是什麼?

回答

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會爲您排列一些靜止圖像請求。