title: AVCaptureVideoPreviewLayer orientation on rotation
body: <p>Simple question, how do I change AVCaptureVideoPreviewLayer orientation when iPhone is rotated from portrait to landscape mode and vice versa. </p>
  
  <p>I know this question has been asked many times and I am not the first one to run into it but I have tried every available solution that I could google or find but it still doesn't work.</p>
  
  <p>The code I have implemented to setup AVCapture is. As you can see I am monitoring orientation changes</p>
  
  <pre><code>-(IBAction)InitiateCamera
  {
  
      [[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications];
      [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(orientationChanged:) name:@"UIDeviceOrientationDidChangeNotification" object:nil];
  
  
     //APPLE CODE
      NSError *error = nil;
  
      // Create the session
      session = [[AVCaptureSession alloc] init];
  
      // Configure the session to produce lower resolution video frames, if your
      // processing algorithm can cope. We'll specify medium quality for the
      // chosen device.
      session.sessionPreset = AVCaptureSessionPresetLow;
  
      // Find a suitable AVCaptureDevice
      device = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo];
  
      // Create a device input with the device and add it to the session.
      AVCaptureDeviceInput *input = [AVCaptureDeviceInput deviceInputWithDevice:device
                                                                          error:&amp;error];
      if (!input) {
          // Handling the error appropriately.
      }
      [session addInput:input];
  
      // Create a VideoDataOutput and add it to the session
      AVCaptureVideoDataOutput *output = [[AVCaptureVideoDataOutput alloc] init];
      [session addOutput:output];
  
      // Configure your output.
      dispatch_queue_t queue = dispatch_queue_create("myQueue", NULL);
      [output setSampleBufferDelegate:self queue:queue];
      dispatch_release(queue);
  
      // Specify the pixel format
      output.videoSettings =
      [NSDictionary dictionaryWithObject:
       [NSNumber numberWithInt:kCVPixelFormatType_32BGRA]
                                  forKey:(id)kCVPixelBufferPixelFormatTypeKey];
  
  
      // If you wish to cap the frame rate to a known value, such as 15 fps, set
      // minFrameDuration.
      output.minFrameDuration = CMTimeMake(1, 15);
  
  
      avLayer = [[AVCaptureVideoPreviewLayer alloc] initWithSession:session];
  
      CGRect bounds=self.view.layer.bounds;
      avLayer.videoGravity = AVLayerVideoGravityResizeAspectFill;
      avLayer.bounds=bounds;
      avLayer.position=CGPointMake(CGRectGetMidX(bounds), CGRectGetMidY(bounds));
  
      //avLayer.backgroundColor = [[UIColor blackColor] CGColor];
      [self.view.layer insertSublayer:avLayer atIndex:1];
  
  
      // Start the session running to start the flow of data
      [session startRunning];
  
      // Assign session to an ivar.
      [self setSession:session];
  
  }
  </code></pre>
  
  <p>I used this post as an answer to try to change my AVCapture view orientation
  <a href="https://stackoverflow.com/questions/12663913/ios-calayer-orientation-avcapturevideopreviewlayer-doesnt-rotate">iOS CAlayer Orientation AVCaptureVideoPreviewLayer doesn&#39;t rotate</a></p>
  
  <pre><code>- (void) orientationChanged:(NSNotification *)notification
  {
      [self rotateLayer];
  }
  
  -(void)rotateLayer
  {
      CGRect layerRect = [[[self view] layer] bounds];
  
      UIDeviceOrientation orientation =[[UIDevice currentDevice]orientation];
  
      switch (orientation) {
          case UIDeviceOrientationLandscapeLeft:
              avLayer.affineTransform = CGAffineTransformMakeRotation(M_PI+ M_PI_2); // 270 degress
  
              break;
          case UIDeviceOrientationLandscapeRight:
              avLayer.affineTransform = CGAffineTransformMakeRotation(M_PI_2); // 90 degrees
              break;
          case UIDeviceOrientationPortraitUpsideDown:
              avLayer.affineTransform = CGAffineTransformMakeRotation(M_PI); // 180 degrees
              break;
          default:
              avLayer.affineTransform = CGAffineTransformMakeRotation(0.0);
              [avLayer setBounds:layerRect];
              break;
      }
      [avLayer setPosition:CGPointMake(CGRectGetMidX(layerRect),CGRectGetMidY(layerRect))];
  
  }
  </code></pre>
  
  <p>The problem is that as you can see my screenshots, initially everything shows up correctly but after rotating the device the AVCapture view is all wacked out. Any suggestions as to what I am doing wrong in rotateLayer?</p>
  
  <p><img src="https://i.stack.imgur.com/6hJVs.png" alt="enter image description here">
  <img src="https://i.stack.imgur.com/2zNYk.png" alt="enter image description here">
  <img src="https://i.stack.imgur.com/jiqRJ.png" alt="enter image description here"></p>