AVFoundation - How to control exposure

前端 未结 2 1612
Happy的楠姐
Happy的楠姐 2021-01-18 00:07

AFTER tapping to take picture, I want to lock exposure and turn off torch as soon as exposure is no longer adjusting. So, I added an observer to handle adj

2条回答
  •  有刺的猬
    2021-01-18 00:34

    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.

提交回复
热议问题