title: iOS NSInternalInconsistencyException while taking a photo while app is using iOS9 picture in picture mode
body: <p>I am having a crash when trying to take a photo (front facing camera), that only fails when the user is using picture in picture mode for a separate apps video. Everything works fine if the user doesn't have a video in picture in picture. The crash occurs on this line:</p>
  
  <pre><code>[stillImageOutput captureStillImageAsynchronouslyFromConnection:videoConnection completionHandler: ^(CMSampleBufferRef imageSampleBuffer, NSError *error) {
  </code></pre>
  
  <p>with the error</p>
  
  <pre><code>*** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: '*** -[AVCaptureStillImageOutput captureStillImageAsynchronouslyFromConnection:completionHandler:] - inconsistent state.'
  </code></pre>
  
  <p>I tried checking if the phone just couldn't take a photo in general while using picture in picture mode, but the default iOS camera app is able to take a picture fine (although maybe it uses a different method of taking the photo). <code>stillImageOutput</code> and <code>videoConnection</code> seem to be set up fine and are not nil. </p>
  
  <p>Here is the code leading up to this crash in case it helps.</p>
  
  <pre><code>avCaptureSession = [[AVCaptureSession alloc] init];
  AVCaptureDevice* cameraDevice = [GS60_FriendFeed_ScreenshotSelfie_Preview_View frontFacingCameraIfAvailable];
  avCaptureSession.sessionPreset = avCaptureSessionPresetString;
  
  NSError *error = nil;
  AVCaptureDeviceInput *input = [AVCaptureDeviceInput deviceInputWithDevice:cameraDevice error:&amp;error];
  if (!input) {
      NSLog(@"ERROR: trying to open camera: %@", error);
  }
  [avCaptureSession addInput:input];
  AVCaptureStillImageOutput* stillImageOutput = [[AVCaptureStillImageOutput alloc] init];
  NSDictionary *outputSettings = [[NSDictionary alloc] initWithObjectsAndKeys: AVVideoCodecJPEG, AVVideoCodecKey, nil];
  [stillImageOutput setOutputSettings:outputSettings];
  [avCaptureSession addOutput:stillImageOutput];
  [avCaptureSession startRunning];
  </code></pre>
  
  <p>and later</p>
  
  <pre><code>AVCaptureConnection* videoConnection = nil;
  AVCaptureStillImageOutput* stillImageOutput = [[avCaptureSession outputs] objectAtIndex:0];
  for (AVCaptureConnection* connection in stillImageOutput.connections) {
      for (AVCaptureInputPort *port in [connection inputPorts]) {
          if ([[port mediaType] isEqual:AVMediaTypeVideo] ) {
              videoConnection = connection;
              break;
          }
      }
      if (videoConnection) { break; }
  }
  
  UIInterfaceOrientation orientation = [UIApplication sharedApplication].statusBarOrientation;
  AVCaptureVideoOrientation avcaptureOrientation = AVCaptureVideoOrientationPortrait;
  if(orientation == UIInterfaceOrientationUnknown) {
      avcaptureOrientation = AVCaptureVideoOrientationPortrait;
  } else if(orientation == UIInterfaceOrientationPortrait) {
      avcaptureOrientation = AVCaptureVideoOrientationPortrait;
  } else if(orientation == UIInterfaceOrientationPortraitUpsideDown) {
      avcaptureOrientation = AVCaptureVideoOrientationPortraitUpsideDown;
  } else if(orientation == UIInterfaceOrientationLandscapeLeft) {
      avcaptureOrientation = AVCaptureVideoOrientationLandscapeLeft;
  } else if(orientation == UIInterfaceOrientationLandscapeRight) {
      avcaptureOrientation = AVCaptureVideoOrientationLandscapeRight;
  }
  [videoConnection setVideoOrientation:avcaptureOrientation];
  
  //this line flips the image so it uses exactly what the preview shows
  [videoConnection setVideoMirrored:YES];
  [stillImageOutput captureStillImageAsynchronouslyFromConnection:videoConnection completionHandler: ^(CMSampleBufferRef imageSampleBuffer, NSError *error) {
  ...
  </code></pre>
  
  <p>I would prefer to be able to take the photo but if that is not possible while picture in picture is open, knowing how to detect that we won't be able to take it would be still be helpful.</p>
  
  <p>Thanks for any help.</p>