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

4

我正在尝试使用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 et al 表明焦点已成功锁定。
我尝试使用 [device unlockForConfiguration][device unlockForConfiguration] 包装锁定代码,但这并没有解决问题。
请问有人能够找出我的代码错误或者我遗漏了什么步骤吗?(我知道我也可以使用视频捕获代替静态捕获,但我需要 AVCaptureSessionPresetPhoto 分辨率的图像。)非常感谢任何帮助和支持。谢谢。
1个回答

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或应用程序响应性造成任何问题。)


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接