Locked all the settings in AVFoundation but brightness sometimes still changes.

I locked all the setting I could find in AVFoundation, including focus, exposure and white balance. In theory, for one object, the photos should be exactly the same in the same light environment.


However, the brightness of photos depended on the initial light environment. For example, if I point the camera to the sun at the beginning, after I moved it back to the test environment and take a picture, the pictures will be darker than the ones taken directly in the test environment.


I checked the exif data of the photos and found that the settings were locked (namely, same).


Thanks in advance for any suggestion.

We once had an issue similar to what you are describing (sometime after the release of iOS 8) but were able to resolve it by following the proper procedure, as outlined in WWDC 2014 Session 508.


Let's assume you know the manual settings (exposure duration, ISO, white balance gains, lens position) for your specific test environment.

You can lock the camera to those specific settings as follows:


AVCaptureDevice* device = [ ... your capture device ... ];
[device lockForConfiguration:nil];

CMTime manualExposureDuration = [...];
float manualISO  = [...];
AVCaptureWhiteBalanceGains manualWhiteBalanceGains  = [...];
float manualLensPosition  = [...];

CMTime __block nextValidSampleBufferTime = kCMTimeZero;

[device setExposureModeCustomWithDuration:manualExposureDuration ISO:manualISO completionHandler:^(CMTime syncTime) {
  nextValidSampleBufferTime = CMTimeMaximum(nextValidSampleBufferTime, syncTime);
}];

[device setWhiteBalanceModeLockedWithDeviceWhiteBalanceGains:manualWhiteBalanceGains completionHandler:^(CMTime syncTime) {
  nextValidSampleBufferTime = CMTimeMaximum(nextValidSampleBufferTime, syncTime);
}];

[device setFocusModeLockedWithLensPosition:manualLensPosition completionHandler:^(CMTime syncTime) {
  nextValidSampleBufferTime = CMTimeMaximum(nextValidSampleBufferTime, syncTime);
}];

[device unlockForConfiguration];


// (... later ...)


AVCaptureSession* captureSession = [... your capture session ...];
CMSampleBufferRef photoSampleBuffer = [ ... from AVCaptureVideoDataOutput/AVCapturePhotoOutput/... ]
CMTime sampleBufferTime = CMSyncConvertTime(CMSampleBufferGetPresentationTimeStamp(photoSampleBuffer), captureSession.masterClock, CMClockGetHostTimeClock());
if(CMTimeCompare(sampleBufferTime, nextValidSampleBufferTime) >= 0)
{
  // We have a buffer to which the manual settings have been applied.
}


In our application, we started with auto-exposure/white balance/focus (AVCaptureExposureModeContinuousAutoExposure, AVCaptureWhiteBalanceModeContinuousAutoWhiteBalance, AVCaptureFocusModeContinuousAutoFocus) and stored the current settings for a specific test environment. Later, we would use the code above to restore the those exact settings.


This worked for us and produced consistent, repeatable results.

I do not know how to continue.

Locked all the settings in AVFoundation but brightness sometimes still changes.
 
 
Q