如何使用 captureStillImageAsynchronouslyFromConnection 实现多镜头(iOS AVFoundation)
Posted
技术标签:
【中文标题】如何使用 captureStillImageAsynchronouslyFromConnection 实现多镜头(iOS AVFoundation)【英文标题】:How to implement multi-shot using captureStillImageAsynchronouslyFromConnection (iOS AVFoundation) 【发布时间】:2014-01-28 19:32:44 【问题描述】:我正在尝试使用captureStillImageAsynchronouslyFromConnection
在循环中捕获连续(多张)高分辨率图像,但它偶尔会暂停以重新聚焦。我锁定了对焦模式(如其他 *** 帖子中所述),但这并没有阻止相机偶尔重新对焦。我的代码 sn-p 是:
// [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
表示焦点等应该已成功锁定。
我尝试用[device unlockForConfiguration]
和[device unlockForConfiguration]
包装锁码,但这并没有解决问题。
有人可以识别我的代码中的错误或我缺少的步骤吗? (我意识到我可以使用视频捕获而不是静态捕获来实现这一点,但我需要AVCaptureSessionPresetPhoto
分辨率图像。)任何帮助将不胜感激。谢谢。
【问题讨论】:
【参考方案1】:好的,我找到了问题所在。由于线程和 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 或应用响应造成任何问题。)
【讨论】:
以上是关于如何使用 captureStillImageAsynchronouslyFromConnection 实现多镜头(iOS AVFoundation)的主要内容,如果未能解决你的问题,请参考以下文章
如何在自动布局中使用约束标识符以及如何使用标识符更改约束? [迅速]