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

如何使用captureStillImageAsynchronouslyFromConnection实现多次拍摄(iOS AVFoundation),第1张

概述我试图在循环中使用captureStill ImageAsynchronouslyFromConnection捕获连续(多次拍摄)高分辨率图像,但它偶尔会暂停重新聚焦.我锁定了对焦模式(如其他stackoverflow贴图中所述),但这并没有阻止相机偶尔重新对焦.我的代码片段是: // [self.session beginConfiguration];if ([device lockForCo 我试图在循环中使用captureStill ImageAsynchronouslyFromConnection捕获连续(多次拍摄)高分辨率图像,但它偶尔会暂停重新聚焦.我锁定了对焦模式(如其他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 lockedexposure lockedwhite balance locked

这表明focus等人应该已成功锁定.

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

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

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

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)全部内容,希望文章能够帮你解决如何使用captureStillImageAsynchronouslyFromConnection实现多次拍摄(iOS AVFoundation)所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/1041327.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-24
下一篇 2022-05-24

发表评论

登录后才能评论

评论列表(0条)

保存