1

I am receiving my UIImage's from AVCaptureSession and setting them into UIImageView's. The portrait mode sets images with correct orientation but once I use landscape mode the images are set rotated 90 degrees clockwise.

I did a little research and discovered that I can use UIImageOrientation with this method to resolve my issue:

[UIImage imageWithCGImage:cgimageref scale:1 orientation:orientation]

But I do not understand how. If I look at UIImageOrientation on all images, the value is always 3 (NSLog(@"%d",UIImageOrientation)) in both portrait and landscape modes. I am puzzled about how this can help me. Am I missing something?

Here is orientation code:

-(void) didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation{
  self.prevLayer.frame = self.view.bounds;
  self.prevLayer.orientation = [[UIDevice currentDevice] orientation];
}

shouldAutorotateToInterfaceOrientation returns YES.

Vad
  • 3,658
  • 8
  • 46
  • 81
  • Post your rotation code. Most likely you are circumventing, or preventing the default rotation code from execution in some way. So even though your view appears to be landscape, it may still be rendering in portrait. Also double check your Info.plist and ensure your list of allowed rotations is accurate. – Sam Mar 28 '12 at 18:33
  • I added my rotation code above. Plist looks fine - all orientations are supported. Everything looks normal to me. – Vad Mar 29 '12 at 02:14
  • I just tracked: orientation of device is always correct. It's the image orientation that is always 3 for some reason when picture is taken with AVCaptureSession – Vad Mar 29 '12 at 03:27

1 Answers1

4

The solution took some time to find. I had to add orientation to AVCaptureConnection

AVCaptureConnection *videoConnection = nil;
for (AVCaptureConnection *connection in stillImageOutput.connections){
    for (AVCaptureInputPort *port in [connection inputPorts]){
        if ([[port mediaType] isEqual:AVMediaTypeVideo] ){
            videoConnection = connection;
            break;
        }
    }
    if (videoConnection) { break; }
}

This line below is the fix:

if([videoConnection isVideoOrientationSupported]) {
    [videoConnection setVideoOrientation:[[UIDevice currentDevice] orientation]];
}

And all images work right with any orientation.

Vad
  • 3,658
  • 8
  • 46
  • 81