title: Quick 180 rotation of iOS Device results in the camera viewing upside-down
body: <p>I've implemented the code below to change the orientation of an <code>AVCaptureVideoSession</code> based upon the <code>UIInterfaceOrientation</code>:</p>
  
  <pre><code>- (AVCaptureVideoOrientation)interfaceOrientationToVideoOrientation:(UIInterfaceOrientation)orientation {
      switch (orientation) {
          case UIInterfaceOrientationPortrait:
              return AVCaptureVideoOrientationPortrait;
          case UIInterfaceOrientationPortraitUpsideDown:
              return AVCaptureVideoOrientationPortraitUpsideDown;
          case UIInterfaceOrientationLandscapeLeft:
              return AVCaptureVideoOrientationLandscapeLeft;
          case UIInterfaceOrientationLandscapeRight:
              return AVCaptureVideoOrientationLandscapeRight;
          default:
              break;
      }
      NSLog(@"Warning - Didn't recognise interface orientation (%ld)",(long)orientation);
      return AVCaptureVideoOrientationPortrait;
  }
  </code></pre>
  
  <p>This code works almost perfectly. However, one problem that occurs is that if you quickly rotate the iOS device 180 degrees, the camera will be shown upside down. </p>
  
  <p>Here's what the camera view looks like before the rotation:</p>
  
  <p><img src="https://i.stack.imgur.com/p4LnH.png" alt="enter image description here"></p>
  
  <p>And here's what it looks like after the rotation:</p>
  
  <p><img src="https://i.stack.imgur.com/UJ5KU.png" alt="enter image description here"></p>
  
  <p>Additionally, here is my implementation of <code>viewDidLayoutSubviews</code>: </p>
  
  <pre><code>- (void)viewDidLayoutSubviews {
  
      [super viewDidLayoutSubviews];
  
      previewLayer.frame = self.view.bounds;
  
      self.view.translatesAutoresizingMaskIntoConstraints = NO;
      previewLayer.videoGravity = AVLayerVideoGravityResizeAspectFill;
      [self.view.layer addSublayer:previewLayer];
  
      if (previewLayer.connection.supportsVideoOrientation) {
          previewLayer.connection.videoOrientation = [self interfaceOrientationToVideoOrientation:[UIApplication sharedApplication].statusBarOrientation];
      }
  }
  </code></pre>
  
  <p>Does anyone have an idea of why the camera view would be upside down when this 180-degree rotation occurs?</p>