1

I am almost bald after pulling so much hair trying to figure this thing out. I am trying to snap a photo with avFoundation and the orientation is almost always messed up. I have the following function in my ViewWillAppear to capture device orientation:

- (void)deviceOrientationDidChange { 
  UIDeviceOrientation deviceOrientation = [[UIDevice currentDevice] orientation];

  if (deviceOrientation == UIDeviceOrientationPortrait)
    orientation = AVCaptureVideoOrientationPortrait;
  else if (deviceOrientation == UIDeviceOrientationPortraitUpsideDown)
    orientation = AVCaptureVideoOrientationPortraitUpsideDown;
  // AVCapture and UIDevice have opposite meanings for landscape left and right (AVCapture orientation is the same as UIInterfaceOrientation)
  else if (deviceOrientation == UIDeviceOrientationLandscapeLeft)
    orientation = AVCaptureVideoOrientationLandscapeRight;
  else if (deviceOrientation == UIDeviceOrientationLandscapeRight)
    orientation = AVCaptureVideoOrientationLandscapeLeft;

// Ignore device orientations for which there is no corresponding still image orientation (e.g. UIDeviceOrientationFaceUp)
}

This code seems to work fine and captures the proper orientation. I then run the following code to generate the image:

    [myConnection setVideoOrientation:self.orientation];
    //THIS IS THE CORRECT ORIENTATION
    NSLog(@"ORIENTATION %d",myConnection.videoOrientation);

[self.stillOutput captureStillImageAsynchronouslyFromConnection:myConnection completionHandler:^(CMSampleBufferRef imageSampleBuffer, NSError *error)
 {
         if (imageSampleBuffer != nil) {
         NSData *jpeg = [AVCaptureStillImageOutput jpegStillImageNSDataRepresentation:imageSampleBuffer]; //jpeg image in binary format

         UIImage *image = [[UIImage alloc] initWithData:jpeg];

         NSLog(@"IMAGE ORIENTATION: %d", image.imageOrientation);

         postProcessingBlock(image, mutable);

         [mutable release];
         [image release];
     }
     else {
         NSLog(@"AVCapture ERROR:%@", [error localizedDescription]);
     }
 }];

When I take a photo in portrait mode using the back camera (button down) my Log shows:

ORIENTATION 1
IMAGE ORIENTATION 3

What am I doing wrong? Why is it showing me the wrong orientation?

Thanks!

hb922
  • 989
  • 1
  • 11
  • 23
  • I used an0's solution from this SO: http://stackoverflow.com/questions/5427656/ios-uiimagepickercontroller-result-image-orientation-after-upload/10611036#10611036 – user523234 Sep 05 '12 at 02:34
  • 1
    This wont work, it has the wrong orientation saved on the image, thinking that the image should be rotated even when it shouldnt – hb922 Sep 05 '12 at 17:12
  • I am facing the same problem here. – yong ho Nov 25 '13 at 05:53

1 Answers1

0

Not sure if you figured this out, but you're not doing anything wrong. What is important to note is that AVCaptureVideoOrientation and UIImageOrientation actually refer to different directions!

UIImageOrientationUp actually refers to the device in landscape mode with the volume controls pointed down toward the ground. So UIImageOrientationLeft is the correct orientation for a portrait photo with the power button pointed toward the sky.

mikeho
  • 6,790
  • 3
  • 34
  • 46