AVFoundation - How to control exposure

試著忘記壹切 提交于 2019-12-01 16:43:38

I got something similar to happen by using flash instead of the torch. I have an observer for @"videoDevice.flashActive" as well. I did try using exposureModeLocked first, but it didn't work for me either.

  1. Take a photo with flash on
  2. Then instantly turn flash off and take another photo before the exposure has time to adjust

The code below probably doesn't just work on its own, but it's simplified from what I did.

- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void*)context
{
    if (context == AdjustingExposureContext)
    {
        self.isAdjustingExposure = [change[NSKeyValueChangeNewKey] boolValue];
    }
    else if (context == FlashModeChangedContext)
    {
        self.isFlashActive = [change[NSKeyValueChangeNewKey] boolValue];
        if (!self.flashActive)
        {
            [self captureImage];  // QUICKLY! capture 2nd image without
        }                         // flash before exposure adjusts
    }
    if (!self.isAdjustingExposure && self.flashActive)
    {
        [self removeObserver:self forKeyPath:@"videoDevice.adjustingExposure" context:AdjustingExposureContext];
        [self captureImage];  // capture 1st image with the flash on
    }
}

Now in the callback for captureStillImageAsynchronouslyFromConnection:,

if (self.isFlashActive)
    [self.videoDeviceInput.device setFlashMode:NO];

However, if you need to take more than one photo without flash at the lowered exposure, this strategy may not work.

It is almost certainly a timing issue. Call captureStillImageAsynchronouslyFromConnection:completionHandler: inside your if block. Then the capture will always be executed after exposure has been locked.

if ([self.inputDevice isExposureModeSupported:AVCaptureExposureModeLocked]) {
    dispatch_async(dispatch_get_main_queue(), 
        ^{
            NSError *error = nil;
            if ([self.inputDevice lockForConfiguration:&error]) {
                //code to lock exposure here
                //take photo here
            }
        });
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!