title: Camera feed slow to load with AVCaptureSession on iOS - How can I speed it up?
body: <p>Right now I'm trying to allow users to take pictures in my app without using UIImagePickerController. I'm using AVCaptureSession and all the related classes to load a camera feed as a sublayer on a full-screen view I have on one of my view controllers. The code works but unfortunately the camera is very slow to load. Usually takes 2-3 seconds. Here is my code:</p>
  
  <pre><code>session = [[AVCaptureSession alloc] init];
  session.sessionPreset = AVCaptureSessionPresetMedium;
  
  if ([session canSetSessionPreset:AVCaptureSessionPresetHigh])
      //Check size based configs are supported before setting them
      [session setSessionPreset:AVCaptureSessionPresetHigh];
  
  [session setSessionPreset:AVCaptureSessionPreset1280x720];
  
  CALayer *viewLayer = self.liveCameraFeed.layer;
  //NSLog(@"viewLayer = %@", viewLayer);
  
  AVCaptureVideoPreviewLayer *captureVideoPreviewLayer = [[AVCaptureVideoPreviewLayer alloc] initWithSession:session];
  
  captureVideoPreviewLayer.frame = viewLayer.bounds;
  [viewLayer addSublayer:captureVideoPreviewLayer];
  
  AVCaptureDevice *device;
  
  if(isFront)
  {
      device = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo];
  }
  else
  {
      device = [self frontCamera];
  }
  
  AVCaptureDevice *audioDevice = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeAudio];
  AVCaptureDeviceInput * audioInput = [AVCaptureDeviceInput deviceInputWithDevice:audioDevice error:nil];
  [session addInput:audioInput];
  
  NSError *error = nil;
  input = [AVCaptureDeviceInput deviceInputWithDevice:device error:&amp;error];
  if (!input) {
      // Handle the error appropriately.
      //NSLog(@"ERROR: trying to open camera: %@", error);
  }
  
  [session addInput:input];
  [session startRunning];
  
  stillImageOutput = [[AVCaptureStillImageOutput alloc] init];
  NSDictionary *outputSettings = [[NSDictionary alloc] initWithObjectsAndKeys: AVVideoCodecJPEG, AVVideoCodecKey, nil];
  [stillImageOutput setOutputSettings:outputSettings];
  [session addOutput:stillImageOutput];
  </code></pre>
  
  <p>Is there any way to speed it up? I've already tried loading it on another thread using Grand Central Dispatch and NSThread and though that stopped the app from freezing it made the loading of the camera take even longer. Any help is appreciated.</p>