UIInterfaceOrientation是Landscape,但UIView仍处于Portrait模式

时间:2013-03-15 10:59:09

标签: ios avcapturesession

我正在使用最新的SDK开发iOS应用程序。

我正在开发一款仅支持LandscapeRight方向的相机应用程序。

在主要ViewController上我为UIView添加了AVCaptureVideoPreviewLayer。换句话说,主ViewController我有主视图,另一个视图videoPreviewViewAVCaptureVideoPreviewLayer

我在AVCaptureVideoPreviewLayer方法上设置了viewWillAppear:

- (void) viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    [self setUpVideo];
}

- (void)setUpVideo
{
    NSLog(@"Set up video");
    if (DataExchanger.cameraManager != nil)
    {
        UIView *view = [self videoPreviewView];
        CALayer *viewLayer = [view layer];
        [viewLayer setMasksToBounds:YES];

        AVCaptureVideoPreviewLayer *newCaptureVideoPreviewLayer = [[AVCaptureVideoPreviewLayer alloc] initWithSession:DataExchanger.cameraManager.captureSession];
        CGRect bounds = [view bounds];
        NSLog(@"BOUNDS: %@", NSStringFromCGRect(bounds));

        [newCaptureVideoPreviewLayer setFrame:bounds];
        [newCaptureVideoPreviewLayer setVideoGravity:AVLayerVideoGravityResizeAspectFill];

        [viewLayer insertSublayer:newCaptureVideoPreviewLayer below:[[viewLayer sublayers] objectAtIndex:0]];
    }
}

我得到这个日志:

BOUNDS: {{0, 0}, {320, 480}}

但是UIInterfaceOrientationUIInterfaceOrientationLandscapeRight,我会得到这些值:

BOUNDS: {{0, 0}, {480, 320}}

另一个问题是我看到视频旋转-90度

我找到了这个stackoverflow question,但永远不会触发- (void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation方法。

发生了什么事?我怎样才能解决这个问题?

1 个答案:

答案 0 :(得分:-1)

  1. 默认的AVCaptureVideoPreviewLayer对象方向为landscape!
  2. AVCaptureVideoPreviewLayer的方向不会随着设备的方向而改变。你必须手动更改它。
  3. 要手动更改,您需要调用预览图层的连接并旋转它:
  4. iOS6的:

     AVCaptureConnection *previewLayerConnection=self.newCaptureVideoPreviewLayer.connection;
     if ([previewLayerConnection isVideoOrientationSupported])
       [previewLayerConnection setVideoOrientation:AVCaptureVideoOrientationLandscapeRight];
    

    < iOS6的:

    if ([self.newCaptureVideoPreviewLayer isOrientationSupported])   
    [self.newCaptureVideoPreviewLayer setOrientation:AVCaptureVideoOrientationLandscapeRight];