title: Switch cameras with avcapturesession
body: <p>Using this tutorial here: <a href="http://www.musicalgeometry.com/?p=1297" rel="noreferrer">http://www.musicalgeometry.com/?p=1297</a> I have created a custom overlay and image capture with <code>AVCaptureSession</code>.</p>
  
  <p>I am attempting to allow the user to switch between the front and back camera. Here is my code in <code>CaptureSessionManager</code> to switch cameras:</p>
  
  <pre><code>- (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];
              } 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];
              } else {
                  NSLog(@"Couldn't add back facing video input");
              }
          }
      }
  }
  </code></pre>
  
  <p>Now in my custom overlay controller I initialize everything like so in <code>viewDidLoad</code>:</p>
  
  <pre><code>[self setCaptureManager:[[CaptureSessionManager alloc] init]];
  
  [[self captureManager] addVideoInputFrontCamera:NO]; // set to YES for Front Camera, No for Back camera
  
  [[self captureManager] addStillImageOutput];
  
  [[self captureManager] addVideoPreviewLayer];
  CGRect layerRect = [[[self view] layer] bounds];
  [[[self captureManager] previewLayer] setBounds:layerRect];
  [[[self captureManager] previewLayer] setPosition:CGPointMake(CGRectGetMidX(layerRect),CGRectGetMidY(layerRect))];
  [[[self view] layer] addSublayer:[[self captureManager] previewLayer]];
  
  [[_captureManager captureSession] startRunning];
  </code></pre>
  
  <p>The switch camera button is connected to a method called <code>switchCamera</code>. I have tried this:</p>
  
  <pre><code>- (void)switchCameraView:(id)sender {
  
      [[self captureManager] addVideoInputFrontCamera:YES]; // set to YES for Front Camera, No for Back camera
  
  }
  </code></pre>
  
  <p>When calling this, I get the error <code>NSLog</code> from the <code>CaptureSessionManager</code> and I cannot figure out why. In <code>viewDidLoad</code>, if I set the <code>fontCamera</code> to <code>YES</code>, it shows the front camera but cannot switch to back, and vice versa.</p>
  
  <p>Any ideas on how to get it to switch properly?</p>