如何使用captureStillImageAsynchronouslyFromConnection实现多次拍摄(iOS AVFoundation)

时间:2014-01-28 19:32:44

标签: ios iphone cocoa-touch avfoundation avcapturesession

我正在尝试在循环中使用captureStillImageAsynchronouslyFromConnection捕捉连续(多次拍摄)高分辨率图像,但偶尔会暂停重新对焦。我锁定了对焦模式(如其他stackoverflow贴图中所述),但这并没有阻止相机偶尔重新对焦。我的代码段是:

// [self.session beginConfiguration];
if ([device lockForConfiguration:nil] == YES) {
    if ([device isFocusModeSupported:AVCaptureFocusModeLocked]) {
        [device setFocusMode:AVCaptureFocusModeLocked];
        NSLog(@"focus locked");
    }
    if ([device isExposureModeSupported:AVCaptureExposureModeLocked]) {
        [device setExposureMode:AVCaptureExposureModeLocked];
        NSLog(@"exposure locked");
    }
    if ([device isWhiteBalanceModeSupported:AVCaptureWhiteBalanceModeLocked]) {
        [device setWhiteBalanceMode:AVCaptureWhiteBalanceModeLocked];
        NSLog(@"white balance locked");
    }
}
// [self.session commitConfiguration];

for (int n = 0; n < 5; n++) {
    [self.stillImageOutput captureStillImageAsynchronouslyFromConnection:[self.stillImageOutput connectionWithMediaType:AVMediaTypeVideo] completionHandler:^(CMSampleBufferRef imageDataSampleBuffer, NSError *error) {

        if (imageDataSampleBuffer) {
            NSData *imageData = [AVCaptureStillImageOutput jpegStillImageNSDataRepresentation:imageDataSampleBuffer];
            UIImage *image = [[UIImage alloc] initWithData:imageData];
            [[[ALAssetsLibrary alloc] init] writeImageToSavedPhotosAlbum:[image CGImage] orientation:(ALAssetOrientation)image.imageOrientation completionBlock:nil];
        }
    }];
}

[device unlockForConfiguration]

输出日志报告:

focus locked
exposure locked
white balance locked

表示focus等人应该已成功锁定。

我尝试使用[device unlockForConfiguration][device unlockForConfiguration]包装锁码,但这并没有解决问题。

有人可以识别我的代码中的错误或我错过的一个步骤吗? (我意识到我可以使用视频捕获而不是仍然捕获来实现这一点,但我需要AVCaptureSessionPresetPhoto分辨率图像。)任何帮助将不胜感激。谢谢。

1 个答案:

答案 0 :(得分:3)

好的,我发现了问题。由于线程和GCD任务的时间安排,所有[device unlockForConfiguration]调用完成之前captureStillImageAsynchronouslyFromConnection正在执行。一个快速的解决方案是添加一个自旋锁,例如:

if ([device lockForConfiguration:nil]) {
    if ([device isFocusModeSupported:AVCaptureFocusModeLocked])
        [device setFocusMode:AVCaptureFocusModeLocked];
    if ([device isExposureModeSupported:AVCaptureExposureModeLocked])
        [device setExposureMode:AVCaptureExposureModeLocked];
    if ([device isWhiteBalanceModeSupported:AVCaptureWhiteBalanceModeLocked])
        [device setWhiteBalanceMode:AVCaptureWhiteBalanceModeLocked];
}

__block int photoCount = 5; 
for (int n = photoCount; n > 0; n--) {
    [self.stillImageOutput captureStillImageAsynchronouslyFromConnection:[self.stillImageOutput connectionWithMediaType:AVMediaTypeVideo] completionHandler:^(CMSampleBufferRef imageDataSampleBuffer, NSError *error) {
        @synchronize(self) {
            photoCount--;
        }

        if (imageDataSampleBuffer) {
            NSData *imageData = [AVCaptureStillImageOutput jpegStillImageNSDataRepresentation:imageDataSampleBuffer];
            UIImage *image = [[UIImage alloc] initWithData:imageData];
            [[[ALAssetsLibrary alloc] init] writeImageToSavedPhotosAlbum:[image CGImage] orientation:(ALAssetOrientation)image.imageOrientation completionBlock:nil];
        }
    }];
}

while (photoCount > 0); // Spinlock until captureStillImageAsynchronouslyFromConnection captured all photos into memory
[device unlockForConfiguration]

那里可能有更多优雅的解决方案(我很乐意听到它们),但是一个简单的自旋锁就可以了。 (此外,由于我的代码在dispatch_async块中运行,因此它不会导致UI或应用程序响应性出现任何问题。)