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!