How to implement multitasking using captureStillImageAsynchronouslyFromConnection (iOS AVFoundation)

I try to capture continuous (multi-tasking) high-resolution images using captureStillImageAsynchronouslyFromConnectionin a loop, but sometimes it pauses for reorientation. I blocked the focus mode (as described in other postoverflow postings), but this did not prevent the camera from refocusing periodically. My code snippet:

// [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]

Output Log Report:

focus locked
exposure locked
white balance locked

which indicates that focus and others should be successfully locked.

I tried to wrap the lock code with [device unlockForConfiguration]and [device unlockForConfiguration], but this did not fix the problem.

- - , ? ( , , , , AVCaptureSessionPresetPhoto.) . .

+4
1

, . [device unlockForConfiguration] , captureStillImageAsynchronouslyFromConnection - GCD. -, :

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, .)

+3

Source: https://habr.com/ru/post/1524146/


All Articles