Question

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.

Was it helpful?

Solution

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top