title: AVCaptureSession rotation
body: <p>I'm trying to get my app to create a UIImage the correct way round.</p>
  
  <p>Most of my code is taken from Apple examples...</p>
  
  <pre><code>@interface CameraManager () &lt;AVCaptureVideoDataOutputSampleBufferDelegate&gt;
  
  @property (nonatomic, strong) CIContext *context;
  @property (nonatomic, strong) AVCaptureDevice *rearCamera;
  
  @end
  
  @implementation CameraManager
  
  - (id)init {
      if ((self = [super init])) {
  
          self.context = [CIContext contextWithOptions:nil];
          [self setupCamera];
          [self addStillImageOutput];
      }
      return self;
  }
  
  - (void)setupCamera
  {
      self.session = [[AVCaptureSession alloc] init];
      [self.session beginConfiguration];
  
      [self.session setSessionPreset:AVCaptureSessionPresetPhoto];
  
      NSArray *devices = [AVCaptureDevice devicesWithMediaType:AVMediaTypeVideo];
      self.rearCamera = nil;
      for (AVCaptureDevice *device in devices) {
          if (device.position == AVCaptureDevicePositionBack) {
              self.rearCamera = device;
              break;
          }
      }
  
      NSError *error = nil;
      AVCaptureDeviceInput *input = [AVCaptureDeviceInput deviceInputWithDevice:self.rearCamera error:&amp;error];
      [self.session addInput:input];
  
      AVCaptureVideoDataOutput *dataOutput = [[AVCaptureVideoDataOutput alloc] init];
      [dataOutput setAlwaysDiscardsLateVideoFrames:YES];
  
      NSDictionary *options = @{(id)kCVPixelBufferPixelFormatTypeKey : @(kCVPixelFormatType_32BGRA)};
      [dataOutput setVideoSettings:options];
  
      [dataOutput setSampleBufferDelegate:self queue:dispatch_get_main_queue()];
  
      [self.session addOutput:dataOutput];
      [self.session commitConfiguration];
  }
  
  - (void)captureOutput:(AVCaptureOutput *)captureOutput didOutputSampleBuffer:(CMSampleBufferRef)sampleBuffer fromConnection:(AVCaptureConnection *)connection
  {
      // grab the pixel buffer
      CVPixelBufferRef pixelBuffer = (CVPixelBufferRef) CMSampleBufferGetImageBuffer(sampleBuffer);
  
      // create a CIImage from it, rotate it and zero the origin
      CIImage *image = [CIImage imageWithCVPixelBuffer:pixelBuffer];
      if ([[UIApplication sharedApplication] statusBarOrientation] == UIInterfaceOrientationLandscapeLeft) {
          image = [image imageByApplyingTransform:CGAffineTransformMakeRotation(M_PI)];
      }
      CGPoint origin = [image extent].origin;
      image = [image imageByApplyingTransform:CGAffineTransformMakeTranslation(-origin.x, -origin.y)];
  
      // set it as the contents of the UIImageView
      CGImageRef cgImage = [self.context createCGImage:image fromRect:[image extent]];
      UIImage *uiImage = [UIImage imageWithCGImage:cgImage];
  
      [[NSNotificationCenter defaultCenter] postNotificationName:@"image" object:uiImage];
  
      CGImageRelease(cgImage);
  }
  
  - (void)addStillImageOutput
  {
      [self setStillImageOutput:[[AVCaptureStillImageOutput alloc] init]];
      NSDictionary *outputSettings = [[NSDictionary alloc] initWithObjectsAndKeys:AVVideoCodecJPEG,AVVideoCodecKey,nil];
      [[self stillImageOutput] setOutputSettings:outputSettings];
  
      AVCaptureConnection *videoConnection = nil;
      for (AVCaptureConnection *connection in [self.stillImageOutput connections]) {
          for (AVCaptureInputPort *port in [connection inputPorts]) {
              if ([[port mediaType] isEqual:AVMediaTypeVideo] ) {
                  videoConnection = connection;
                  break;
              }
          }
          if (videoConnection) {
              break;
          }
      }
  
      [[self session] addOutput:[self stillImageOutput]];
  }
  
  - (void)captureStillImage
  {
      AVCaptureConnection *videoConnection = nil;
      for (AVCaptureConnection *connection in [[self stillImageOutput] connections]) {
          for (AVCaptureInputPort *port in [connection inputPorts]) {
              if ([[port mediaType] isEqual:AVMediaTypeVideo]) {
                  videoConnection = connection;
                  break;
              }
          }
          if (videoConnection) {
              break;
          }
      }
  
      NSLog(@"about to request a capture from: %@", [self stillImageOutput]);
      [[self stillImageOutput] captureStillImageAsynchronouslyFromConnection:videoConnection
                                                           completionHandler:^(CMSampleBufferRef imageSampleBuffer, NSError *error) {
                                                               CFDictionaryRef exifAttachments = CMGetAttachment(imageSampleBuffer, kCGImagePropertyExifDictionary, NULL);
                                                               if (exifAttachments) {
                                                                   NSLog(@"attachements: %@", exifAttachments);
                                                               } else {
                                                                   NSLog(@"no attachments");
                                                               }
                                                               NSData *imageData = [AVCaptureStillImageOutput jpegStillImageNSDataRepresentation:imageSampleBuffer];
                                                               UIImage *image = [[UIImage alloc] initWithData:imageData];
  
                                                               [[NSNotificationCenter defaultCenter] postNotificationName:kImageCapturedSuccessfully object:image];
                                                           }];
  }
  </code></pre>
  
  <p>This is my camera manager class code.</p>
  
  <p>I am displaying the preview of the camera using the OutputSampleBufferDelegate (for various reasons).</p>
  
  <p>I'm using the session output to "take a photo".</p>
  
  <p>The method <code>captureStillImage</code> is the bit I'm trying to fix.</p>
  
  <p>The photos are taken with the device in LandscapeLeft orientation (the interface is also LandscapeLeft).</p>
  
  <p>The previews all show the correct way around and the exif data shows the width and height the correct way around too. (X = 3264, Y = 2448).</p>
  
  <p>But when I display the UIImage it is rotated 90 degrees counter clockwise. The aspect ratio of the image is correct (i.e. everything looks fine, circles are still circles) just the rotation.</p>
  
  <p>I have found several categories that claim to fix this.</p>
  
  <p>I have also found several StackOverflow questions with answers that also claim to fix it.</p>
  
  <p>None of these worked.</p>
  
  <p>Does anyone know how to rotate this thing the right way around?</p>