开发者

iPhone App - Show AVFoundation video on landscape mode

开发者 https://www.devze.com 2023-03-11 04:09 出处:网络
I am using the AVCam example App from Apple. This example uses AVFoundation in order to show video on a view.

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 A开发者_C百科VCam 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?


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.


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;


Declare

- (BOOL)shouldAutorotate;

in your .h .

Then do:

- (BOOL)shouldAutorotate {
    return NO;
}

in your .m

This will force it to not rotate.

0

精彩评论

暂无评论...
验证码 换一张
取 消