AVCaptureVideoPreviewLayer方向 - 需要景观 [英] AVCaptureVideoPreviewLayer orientation - need landscape

查看:653
本文介绍了AVCaptureVideoPreviewLayer方向 - 需要景观的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的应用只是风景。我将这样呈现AVCaptureVideoPreviewLayer:

My app is landscape only. I'm presenting the AVCaptureVideoPreviewLayer like this:

self.previewLayer = [[AVCaptureVideoPreviewLayer alloc] initWithSession:session];
[self.previewLayer setBackgroundColor:[[UIColor blackColor] CGColor]];
[self.previewLayer setVideoGravity:AVLayerVideoGravityResizeAspect];                    
NSLog(@"previewView: %@", self.previewView);
CALayer *rootLayer = [self.previewView layer];
[rootLayer setMasksToBounds:YES];
[self.previewLayer setFrame:[rootLayer bounds]];
    NSLog(@"previewlayer: %f, %f, %f, %f", self.previewLayer.frame.origin.x, self.previewLayer.frame.origin.y, self.previewLayer.frame.size.width, self.previewLayer.frame.size.height);
[rootLayer addSublayer:self.previewLayer];
[session startRunning];

self.previewView的框架为(0,0,568,320),这是正确的。 self.previewLayer记录一个(0,0,568,320)的帧,这在理论上是正确的。但是,相机显示在横向屏幕中间显示为纵向矩形,并且相机预览图像的方向错误90度。我究竟做错了什么?我需要在横屏模式下以全屏模式显示相机预览图层,图像应正确定位。

self.previewView has a frame of (0,0,568,320), which is correct. self.previewLayer logs a frame of (0,0,568,320), which is theoretically correct. However, the camera display appears as a portrait rectangle in the middle of the landscape screen, and the orientation of the camera preview image is wrong by 90 degrees. What am I doing wrong? I need the camera preview layer to appear in the full screen, in landscape mode, and the image should be orientated correctly.

推荐答案

默认摄像机方向为横向左侧(左侧是主页按钮)。你需要在这里做两件事:

The default camera orientation is Landscape Left (home button one the left). You need to do two things here:

1-将previewLayer框架更改为:

1- Change the previewLayer frame to:

self.previewLayer.frame=self.view.bounds;

您需要将预览图层框架设置为屏幕边界,以便预览框架屏幕旋转时图层会更改(您不能使用根视图的框架,因为它不会随着旋转而改变,但根视图的边界会改变)。在您的示例中,您将预览层框架设置为我看不到的previewView属性。

You need to set the preview layer frame to the bounds of the screen so that the frame of the preview layer changes when the screen rotates (you cannot use frame of the root view because that does not change with rotation but the bounds of the root view do). In your example, you are setting the previewlayer frame to a previewView property which I do not see.

2-您需要旋转预览图层连接旋转设备。在viewDidAppear中添加此代码:

2- You need to rotate the preview layer connection with the rotation of the device. Add this code in viewDidAppear:

-(void) viewDidAppear:(BOOL)animated
{
  [super viewDidAppear:YES];

  //Get Preview Layer connection
  AVCaptureConnection *previewLayerConnection=self.previewLayer.connection;

  if ([previewLayerConnection isVideoOrientationSupported])
    [previewLayerConnection setVideoOrientation:[[UIApplication sharedApplication] statusBarOrientation]]; 
}

希望这可以解决它。

完全披露:这是一个简化版本,因为您不关心横向右侧还是横向左侧。

Full Disclosure: This is a simplified version since you do not care if Landscape right or Landscape left.

这篇关于AVCaptureVideoPreviewLayer方向 - 需要景观的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆