gpt4 book ai didi

ios - 如何使用 captureStillImageAsynchronouslyFromConnection 实现多重拍摄(iOS AVFoundation)

转载 作者:可可西里 更新时间:2023-11-01 05:44:36 26 4
gpt4 key购买 nike

我正在尝试在循环中使用 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 分辨率图像。)任何帮助将不胜感激。谢谢。

最佳答案

好的,我想出了问题。由于线程和 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 block 中运行,因此它不会导致 UI 或应用程序响应出现任何问题。)

关于ios - 如何使用 captureStillImageAsynchronouslyFromConnection 实现多重拍摄(iOS AVFoundation),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21415040/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com