title: iPhone AVFoundation camera orientation
body: <p>I've been tearing my hair out trying to get the AVFoundation camera to capture a picture in the correct orientation (i.e. the device orientation) but I can't get it to work.</p>
  
  <p>I have looked at tutorials, I've watched the WWDC presentation and I've downloaded the WWDC sample program but even that doesn't do it.</p>
  
  <p>The code from my app is...</p>
  
  <pre><code>AVCaptureConnection *videoConnection = [CameraVC connectionWithMediaType:AVMediaTypeVideo fromConnections:[imageCaptureOutput connections]];
  if ([videoConnection isVideoOrientationSupported])
  {
      [videoConnection setVideoOrientation:[UIApplication sharedApplication].statusBarOrientation];
  }
  
  [imageCaptureOutput captureStillImageAsynchronouslyFromConnection:videoConnection
                                                  completionHandler:^(CMSampleBufferRef imageDataSampleBuffer, NSError *error)
  {
      if (imageDataSampleBuffer != NULL)
      {
          //NSLog(@"%d", screenOrientation);
  
          //CMSetAttachment(imageDataSampleBuffer, kCGImagePropertyOrientation, [NSString stringWithFormat:@"%d", screenOrientation], 0);
  
          NSData *imageData = [AVCaptureStillImageOutput jpegStillImageNSDataRepresentation:imageDataSampleBuffer];
          UIImage *image = [[UIImage alloc] initWithData:imageData];
  
          [self processImage:image];
      }
  }];
  </code></pre>
  
  <p>(processImage uses the same writeImage... method as the WWDC code)</p>
  
  <p>and the code from the WWDC app is...</p>
  
  <pre><code>AVCaptureConnection *videoConnection = [AVCamDemoCaptureManager connectionWithMediaType:AVMediaTypeVideo fromConnections:[[self stillImageOutput] connections]];
          if ([videoConnection isVideoOrientationSupported]) {
              [videoConnection setVideoOrientation:AVCaptureVideoOrientationPortrait];
          }
  
  [[self stillImageOutput] captureStillImageAsynchronouslyFromConnection:videoConnection
                                                               completionHandler:^(CMSampleBufferRef imageDataSampleBuffer, NSError *error) {
                                                                   if (imageDataSampleBuffer != NULL) {
                                                                       NSData *imageData = [AVCaptureStillImageOutput jpegStillImageNSDataRepresentation:imageDataSampleBuffer];
                                                                       UIImage *image = [[UIImage alloc] initWithData:imageData];                                                                 
                                                                       ALAssetsLibrary *library = [[ALAssetsLibrary alloc] init];
                                                                       [library writeImageToSavedPhotosAlbum:[image CGImage]
                                                                                                 orientation:(ALAssetOrientation)[image imageOrientation]
                                                                                             completionBlock:^(NSURL *assetURL, NSError *error){
                                                                                                 if (error) {
                                                                                                     id delegate = [self delegate];
                                                                                                     if ([delegate respondsToSelector:@selector(captureStillImageFailedWithError:)]) {
                                                                                                         [delegate captureStillImageFailedWithError:error];
                                                                                                     }                                                                                               
                                                                                                 }
                                                                                             }];
                                                                       [library release];
                                                                       [image release];
                                                                   } else if (error) {
                                                                       id delegate = [self delegate];
                                                                       if ([delegate respondsToSelector:@selector(captureStillImageFailedWithError:)]) {
                                                                           [delegate captureStillImageFailedWithError:error];
                                                                       }
                                                                   }
                                                               }];
  </code></pre>
  
  <p>At the beginning of their code they set the AVOrientation to portrait which seems very odd but I'm trying to get it to detect the device's current orientation and use that.</p>
  
  <p>As you can see I have put [UIApplication sharedApplication]statusBarOrientation to try and get this but it still only save the photos in portrait.</p>
  
  <p>Can anyone offer any help or advice on what I need to be doing?</p>
  
  <p>Thanks!</p>
  
  <p>Oliver</p>