title: setExposureMode xcode iOS 8
body: <p>I am using the following code below to capture an image. Everything works fine but my commands to set the exposure and whitebalance in setCameraSettings() are ignored. They get executed but they have no effect. My command to set the session image resolution works fine.</p>
  
  <pre><code>#import "CaptureSessionManager.h"
  #import &lt;ImageIO/ImageIO.h&gt;
  
  // based on https://github.com/jj0b/AROverlayImageCapture
  
  @implementation CaptureSessionManager
  
  @synthesize captureSession;
  @synthesize previewLayer;
  @synthesize stillImageOutput;
  @synthesize stillImage;
  @synthesize imageWidth;
  @synthesize imageHeight;
  @synthesize imageBrightnessValue;
  @synthesize imageExposureTime;
  @synthesize imageApertureValue;
  @synthesize imageISOSpeedRatings;
  @synthesize playShutterSound;
  /*************************************************************************************/
  - (id)init {
      if ((self = [super init])) {
          AVCaptureSession *session = [[AVCaptureSession alloc] init];
      //        [session beginConfiguration];
          if ([session canSetSessionPreset:AVCaptureSessionPresetHigh]) {
              session.sessionPreset = AVCaptureSessionPresetHigh; // AVCaptureSessionPresetHigh; // AVCaptureSessionPresetLow;
          }
      //        [session commitConfiguration];
          [self setCaptureSession:session];
      } 
      return self;
  }
  /*************************************************************************************/
  - (void)addVideoPreviewLayer {
      [self setPreviewLayer:[[AVCaptureVideoPreviewLayer alloc] initWithSession: [self captureSession]]];
      [[self previewLayer] setVideoGravity:AVLayerVideoGravityResizeAspectFill];
  }
  /*************************************************************************************/
  
  - (void)addVideoInputFrontCamera:(BOOL)front {
      NSArray *devices = [AVCaptureDevice devices];
      AVCaptureDevice *frontCamera;
      AVCaptureDevice *backCamera;
  
      for (AVCaptureDevice *device in devices) {
  
          NSLog(@"Device name: %@", [device localizedName]);
  
          if ([device hasMediaType:AVMediaTypeVideo]) {
  
              if ([device position] == AVCaptureDevicePositionBack) {
                  NSLog(@"Device position : back");
                  backCamera = device;
              }
              else {
                  NSLog(@"Device position : front");
                  frontCamera = device;
              }
          }
      }
  
      NSError *error = nil;
  
      if (front) {
          AVCaptureDeviceInput *frontFacingCameraDeviceInput = [AVCaptureDeviceInput deviceInputWithDevice:frontCamera error:&amp;error];
          if (!error) {
              if ([[self captureSession] canAddInput:frontFacingCameraDeviceInput]) {
                  [[self captureSession] addInput:frontFacingCameraDeviceInput];
                  currentCaptureDevice = frontCamera;
              } else {
                  NSLog(@"Couldn't add front facing video input");
              }
          }
      } else {
          AVCaptureDeviceInput *backFacingCameraDeviceInput = [AVCaptureDeviceInput deviceInputWithDevice:backCamera error:&amp;error];
          if (!error) {
              if ([[self captureSession] canAddInput:backFacingCameraDeviceInput]) {
                  [[self captureSession] addInput:backFacingCameraDeviceInput];
                  currentCaptureDevice = backCamera;
             } else {
                  NSLog(@"Couldn't add back facing video input");
              }
          }
      }
  }
  /*************************************************************************************/
  
  - (void)addStillImageOutput
  {
      [self setStillImageOutput:[[AVCaptureStillImageOutput alloc] init]];
      NSDictionary *outputSettings = [[NSDictionary alloc] initWithObjectsAndKeys:AVVideoCodecJPEG,AVVideoCodecKey,nil];
      [[self stillImageOutput] setOutputSettings:outputSettings];
  
      [[self captureSession] addOutput:[self stillImageOutput]];
  
      for (AVCaptureConnection *connection in [[self stillImageOutput] connections]) {
          for (AVCaptureInputPort *port in [connection inputPorts]) {
              if ([[port mediaType] isEqual:AVMediaTypeVideo]) {
                  videoConnection = connection;
                  [self setCameraSettings];
                  return;
              }
          }
      }
  }
  /*************************************************************************************/
  
  - (void)setCameraSettings:(long)expTime1000thSec iso:(int)isoValue
  {
      if ( currentCaptureDevice ) {
  
          [captureSession beginConfiguration];
  
          NSError *error = nil;
          if ([currentCaptureDevice lockForConfiguration:&amp;error]) {
  
              if ([currentCaptureDevice isExposureModeSupported:AVCaptureExposureModeLocked]) {
  
                  CMTime minTime, maxTime, exposureTime;
  
                  if ( isoValue &lt; minISO ) {
                      isoValue = minISO;
                  } else if ( isoValue &gt; maxISO ) {
                      isoValue = maxISO;
                  }
  
                  exposureTime = CMTimeMake(expTime1000thSec, EXP_TIME_UNIT); // in 1/EXP_TIME_UNIT of a second
                  minTime = currentCaptureDevice.activeFormat.minExposureDuration;
                  maxTime = currentCaptureDevice.activeFormat.maxExposureDuration;
  
                  if ( CMTimeCompare(exposureTime, minTime) &lt; 0 ) {
                      exposureTime = minTime;
                  } else if ( CMTimeCompare(exposureTime, maxTime) &gt; 0 ) {
                      exposureTime = maxTime;
                  }
  
                  NSLog(@"setting exp time to %lld/%d s (want %ld)   iso=%d", exposureTime.value, exposureTime.timescale, expTime1000thSec, isoValue);
                  [currentCaptureDevice setExposureModeCustomWithDuration:exposureTime ISO:isoValue completionHandler:nil];
              }
  
              if (currentCaptureDevice.lowLightBoostSupported) {
                  currentCaptureDevice.automaticallyEnablesLowLightBoostWhenAvailable = NO;
                  NSLog(@"setting automaticallyEnablesLowLightBoostWhenAvailable = NO");
              }
  
              // lock the gains
              if ([currentCaptureDevice isWhiteBalanceModeSupported:AVCaptureWhiteBalanceModeLocked]) {
                  currentCaptureDevice.whiteBalanceMode = AVCaptureWhiteBalanceModeLocked;
                  NSLog(@"setting AVCaptureWhiteBalanceModeLocked");
              }
  
              // set the gains
              AVCaptureWhiteBalanceGains gains;
              gains.redGain = 1.0;
              gains.greenGain = 1.0;
              gains.blueGain = 1.0;
  
              AVCaptureWhiteBalanceGains normalizedGains = [self normalizedGains:gains];
              [currentCaptureDevice setWhiteBalanceModeLockedWithDeviceWhiteBalanceGains:normalizedGains completionHandler:nil];
              NSLog(@"setWhiteBalanceModeLockedWithDeviceWhiteBalanceGains g.red=%.2lf g.green=%.2lf g.blue=%.2lf",
                    normalizedGains.redGain, normalizedGains.greenGain, normalizedGains.blueGain);
  
              [currentCaptureDevice unlockForConfiguration];
          }
          [captureSession commitConfiguration];
      }
  }
  
  /*************************************************************************************/
  
  - (void)captureStillImage
  {
  
      NSLog(@"about to request a capture from: %@", [self stillImageOutput]);
      [[self stillImageOutput] captureStillImageAsynchronouslyFromConnection:videoConnection
                                                           completionHandler:^(CMSampleBufferRef imageSampleBuffer, NSError *error) {
  
                                                               CFDictionaryRef exifAttachments = CMGetAttachment(imageSampleBuffer, kCGImagePropertyExifDictionary, NULL);
                                                               if (exifAttachments) {
                                                                   NSLog(@"attachements: %@", exifAttachments);
                                                               } else {
                                                                   NSLog(@"no attachments");
                                                               }
  
                                                               NSLog(@"name: %@", [currentCaptureDevice localizedName]);
  
                                                               NSData *imageData = [AVCaptureStillImageOutput jpegStillImageNSDataRepresentation:imageSampleBuffer];
                                                               UIImage *image = [[UIImage alloc] initWithData:imageData];
                                                               [self setStillImage:image];
  
                                                               NSDictionary *dict = (__bridge NSDictionary*)exifAttachments;
                                                               NSString *value = [dict objectForKey:@"PixelXDimension"];
                                                               [self setImageWidth:[NSNumber numberWithInt:[value intValue]]];
  
                                                               NSString *value1 = [dict objectForKey:@"PixelYDimension"];
                                                               [self setImageHeight:[NSNumber numberWithInt:[value1 intValue]]];
  
                                                               NSString *value2 = [dict objectForKey:@"BrightnessValue"];
                                                               [self setImageBrightnessValue:[NSNumber numberWithFloat:[value2 floatValue]]];
  
                                                               NSString *value3 = [dict objectForKey:@"ExposureTime"];
                                                               [self setImageExposureTime:[NSNumber numberWithFloat:[value3 floatValue]]];
  
                                                               NSString *value4 = [dict objectForKey:@"ApertureValue"];
                                                               [self setImageApertureValue:[NSNumber numberWithFloat:[value4 floatValue]]];
  
                                                               NSArray *values = [dict objectForKey:@"ISOSpeedRatings"];
                                                               [self setImageISOSpeedRatings:[NSNumber numberWithFloat:[ [values objectAtIndex:0] floatValue]]];
  
                                                               // must be at end
                                                               [[NSNotificationCenter defaultCenter] postNotificationName:kImageCapturedSuccessfully object:nil];
  
                                                           }];
  
  }
  /********************************************************************************/
  
  - (void)dealloc {
  
      [[self captureSession] stopRunning];
  
      //  [super dealloc];
  
  }
  /************************************************************************************/
  @end
  </code></pre>