HTML5   发布时间:2022-04-27  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了如何使用captureStillImageAsynchronouslyFromConnection实现多次拍摄(iOS AVFoundation)大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
我试图在循环中使用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 locked
exposure locked
white 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)所遇到的程序开发问题。

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

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。