2014-01-23 54 views
2

我有一个iPhone相机应用程序,我在其中使用AVCaptureVideoPreviewLayer。当某人拍摄照片/ stillImage时,这会显示在新的ViewController中。一旦这个视图控制器被解雇,魔法就会发生。AVCaptureVideoPreviewLayer,视频方向卡住

整个应用程序正常旋转,两个viewcontroller;但有一个例外。如果我在一个方向上拍摄图片,请在新的ViewController中旋转设备,然后返回到AVCaptureVideoPreviewLayer,除了图像以外,整个图层都沿着正确的方向旋转,因此突然间,输入会横向显示在previewLayer中。

我已经检查过,并尝试设置PreviewLayer的帧,但这一切似乎都很好,我在调试时看到的值都是正确的。这只是显示的图像偏斜。来回旋转设备可以在使用过程中解决这个问题。

有没有人见过这个之前,有没有人有线索如何解决这个问题?

+0

这是iPhone还是iPad?适用于带纵向模式的iphone手柄。如果iPhone视图控制器允许旋转,我已经看到AVCapture发生了一些愚蠢的事情。 –

+0

@SamBudda它是iPhone的,正如我所说的。除了这种情况之外,它是完美的。 – ophychius

回答

2

由于didRotateFromInterfaceOrientation:在iOS的8弃用当界面旋转

- (void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation { 
    self.previewLayer.connection.videoOrientation = self.interfaceOrientation; 
} 
+0

不幸的是,这是行不通的,因为viewController不是当时的活动的,因此这个函数没有被调用。然而,这确实使我指向了正确的方向。解决方案是编写自己的函数,将其从活动视图中传递给方向,并在其顶部的活动ViewController改变方向时通过通知调用它。 – ophychius

0

我以前有过类似的问题。 一旦驳回相机控制器,你可以刷新视图旋转与此:

UIDeviceOrientation orientation = [[UIDevice currentDevice] orientation]; 
UIWindow *window = [[UIApplication sharedApplication] keyWindow]; 
UIView *topView = [window.subviews objectAtIndex:0]; 
[topView removeFromSuperview]; 
[window addSubview:topView];   

因此,除去顶部显示窗口上大多数观点和重置它的窗口,它应该正确刷新视图旋转。

希望它可以帮助

+0

不幸的是,这似乎并没有解决我的问题。谢谢你的努力。 – ophychius

0

也许,不是改变AVCaptureVideoPreviewLayer框架,你可以尝试把它放在一个容器UIView,并将此容器的框架。 (我修正了'VCaptureVideoPreviewLayer this way : sizing directly PreviewLayer had no effect, but sizing its containing UIView`工作的'尺寸'错误)。

或者 也许你可以强制定向您的viewController viewWillAppear(或viewDidAppear?)

- (void)viewWillAppear:(BOOL)animated 
{ 
    [super viewWillAppear:animated]; 
    // try and force current orientation 
    UIApplication *application = [UIApplication sharedApplication]; 
    UIInterfaceOrientation currentOrientation = application.statusBarOrientation; 
    [application setStatusBarOrientation:currentOrientation animated:animated]; 
} 

祝你好运!

1

你可以改变previewLayer的连接的videoOrientation,我改变连接的视频取向viewDidLayoutSubviews

switch ([UIApplication sharedApplication].statusBarOrientation) { 
    case UIInterfaceOrientationPortrait: 
     self.captureVideoPreviewLayer.connection.videoOrientation = AVCaptureVideoOrientationPortrait; 
     break; 
    case UIInterfaceOrientationPortraitUpsideDown: 
     self.captureVideoPreviewLayer.connection.videoOrientation = AVCaptureVideoOrientationPortraitUpsideDown; 
     break; 
    case UIInterfaceOrientationLandscapeLeft: 
     self.captureVideoPreviewLayer.connection.videoOrientation = AVCaptureVideoOrientationLandscapeLeft; 
     break; 
    case UIInterfaceOrientationLandscapeRight: 
     self.captureVideoPreviewLayer.connection.videoOrientation = AVCaptureVideoOrientationLandscapeRight; 
     break; 
    default: 
     break; 
} 
0

由于interfaceOrientation已弃用,最新的swift 2.0解决方案应为:

let previewLayerConnection = self.previewLayer.connection 
let orientation = AVCaptureVideoOrientation(rawValue: UIApplication.sharedApplication().statusBarOrientation.rawValue) 
previewLayerConnection.videoOrientation = orientation!