3

I am using the AVCam example App from Apple.

This example uses AVFoundation in order to show video on a view.

I am trying to make from the AVCam a landscape App with no luck.

When screen orientation changes the video is shown rotated on the view. Is there a way of handling this problem?

CharlesB
  • 86,532
  • 28
  • 194
  • 218
bashan
  • 3,572
  • 6
  • 41
  • 58

3 Answers3

10

When you create your preview layer:

captureVideoPreviewLayer.orientation = UIInterfaceOrientationLandscapeLeft;

And the methods to manage rotations:

-(void)willAnimateRotationToInterfaceOrientation:
        (UIInterfaceOrientation)toInterfaceOrientation 
        duration:(NSTimeInterval)duration {

  [CATransaction begin];
  if (toInterfaceOrientation==UIInterfaceOrientationLandscapeLeft){
    captureVideoPreviewLayer.orientation = UIInterfaceOrientationLandscapeLeft;
  } else {        
    captureVideoPreviewLayer.orientation = UIInterfaceOrientationLandscapeLeft;
  }

 [CATransaction commit];
 [super willAnimateRotationToInterfaceOrientation:toInterfaceOrientation duration:duration];
}

-(BOOL)shouldAutorotateToInterfaceOrientation:
        (UIInterfaceOrientation)interfaceOrientation {

  [[UIApplication sharedApplication] setStatusBarOrientation:UIInterfaceOrientationLandscapeLeft animated:NO];

  return (interfaceOrientation == UIInterfaceOrientationLandscapeLeft);
}

That worked for me.

Shay Erlichmen
  • 31,691
  • 7
  • 68
  • 87
Samssonart
  • 3,443
  • 3
  • 31
  • 41
  • 1
    Note: you will want to [set the videoOrientation property on the connection](http://stackoverflow.com/a/22187500/35690) since the orientation property is deprecated. – Senseful Mar 05 '14 at 02:53
0

Declare

- (BOOL)shouldAutorotate;

in your .h .

Then do:

- (BOOL)shouldAutorotate {
    return NO;
}

in your .m

This will force it to not rotate.

Blisskarthik
  • 1,246
  • 8
  • 20
Jeff Stone
  • 319
  • 1
  • 4
  • 14
0

Are you using the orientation and gravity settings for the preview layer?

previewLayer = [AVCaptureVideoPreviewLayer layerWithSession:session];
previewLayer.frame = CGRectMake(0, 0, 480, 300); 
previewLayer.videoGravity = AVLayerVideoGravityResizeAspectFill;
previewLayer.orientation =  AVCaptureVideoOrientationLandscapeRight;
previewLayer.automaticallyAdjustsMirroring = YES;
Steve McFarlin
  • 3,576
  • 1
  • 25
  • 24