IOS / iPhone的照片突发模式API

我试图在iPhone 5s上以最高分辨率(AVCaptureSessionPresetPhoto)捕捉多张照片。 我尝试使用下面的代码:

dispatch_semaphore_t sync = dispatch_semaphore_create(0); while( [self isBurstModeEnabled] == YES ) { [stillImageOutput captureStillImageAsynchronouslyFromConnection:videoConnection completionHandler: ^(CMSampleBufferRef imageSampleBuffer, NSError *error) { if (imageSampleBuffer != NULL) { NSData *imageData = [AVCaptureStillImageOutput jpegStillImageNSDataRepresentation:imageSampleBuffer]; NSString *videoThumbPath = [NSString stringWithFormat:@"%@/img%d.png", burstFolderPath, index]; [imageData writeToFile:videoThumbPath atomically:YES]; if( 0 == index ) { [self NSLogPrint:[NSString stringWithFormat:@"Created photo at %@",videoThumbPath]]; } } dispatch_semaphore_signal(sync); }]; dispatch_semaphore_wait(sync, DISPATCH_TIME_FOREVER); } 

使用此代码,我可以每秒获得约2张照片,无法接近本机相机应用程序的连拍模式的性能。 我究竟做错了什么? 此外,我尝试使用上面的代码没有信号量,但在那种情况下,我有奇怪的行为,一些照片丢失(img0.png img1.png img3.png将存在,但img2.png将会丢失)。 使用第二种方法,性能会更好,但仍然不能与原生应用程序的性能相提并论(在我的testing中,相机应用程序每秒钟可拍摄约8.4张照片)。

captureStillImageAsynchronouslyFromConnection:completionHandler:不是,我相信,苹果正在使用它的突发模式。

相反,苹果公司正在以全分辨率抓取video帧 (由5s支持)。 就是这样:

AVCaptureDevice将其activeFormat设置为全传感器分辨率,然后从AVCaptureVideoDataOutputSampleBufferDelegatecaptureOutput:didOutputSampleBuffer:fromConnection:抓取并处理每秒10帧,为每个帧抓取触发快门声音。

对于不支持全传感器尺寸分辨率video的设备,以及/或者如果您希望支持比iOS更早的任何设备,则需要有一个后退(低分辨率图像或更慢的突发模式) 7.x版本

请注意,您不能有多个并发使用captureStillImageAsynchronouslyFromConnection:completionHandler:没有一些非常意外的结果。 这就是为什么你应该从前一个的completionHandler (实质上就是你的信号量在做什么)中调用每个迭代。 此外,您可能希望从PNG切换为突发文件格式 – 它节省得非常缓慢,需要大量的系统资源 – 堆叠15或20个PNG可能会导致您一些严重的悲伤!

* 可能是这样做的,因为它当然可以使用私有API来实现相同的最终结果。

在iOS 8及以上版本中使用此方法进行连拍模式:

 - (void)captureStillImageBracketAsynchronouslyFromConnection:(AVCaptureConnection *)connection withSettingsArray:(NSArray *)settings completionHandler:(void (^)(CMSampleBufferRef sampleBuffer, AVCaptureBracketedStillImageSettings *stillImageSettings, NSError *error))handler NS_AVAILABLE_IOS(8_0); 

文档