I'm trying to capture continuous (multi-shot) high-res images using captureStillImageAsynchronouslyFromConnection
in a loop, but it occasionally pauses to refocus. I locked the focus mode (as described in other stackoverflow postings), but that didn't prevent the camera from occasionally refocusing. My code snippet is:
// [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]
The output log reports:
focus locked
exposure locked
white balance locked
which indicates that focus et al should have been successfully locked.
I tried wrapping the lock code with [device unlockForConfiguration]
and [device unlockForConfiguration]
, but that didn't fix the issue.
Can someone identify an error in my code or a step that I'm missing? (I realize I can alternatively implement this using video capture instead of still capture, but I need AVCaptureSessionPresetPhoto
resolution images.) Any help would be greatly appreciated. Thank you.