我有一个iPhone相机应用程序,我在其中使用AVCaptureVideoPreviewLayer。当有人拍摄照片/ stillImage时,会在新的ViewController中显示。一旦这个视图控制器被解除,就会发生魔术。
整个app正常旋转,都是viewcontroller;但是有一个例外。如果我以一个方向拍摄照片,在新的ViewController中旋转设备,然后返回到AVCaptureVideoPreviewLayer,整个图层旋转得很好,除了图像,所以突然输入通过previewLayer横向呈现。
我已经检查过,并尝试为PreviewLayer设置框架,但这一切似乎都很好,我在调试时看到的值都是正确的。只是显示的图像是倾斜的。在使用过程中来回旋转设备可以解决这个问题。
有没有人见过这个,有没有人知道如何解决这个问题?
答案 0 :(得分:2)
您可以在界面旋转时更改previewLayer连接的videoOrientation
- (void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation {
self.previewLayer.connection.videoOrientation = self.interfaceOrientation;
}
答案 1 :(得分:1)
由于iOS 8中已弃用didRotateFromInterfaceOrientation:
,因此我在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;
}
答案 2 :(得分:0)
过去我遇到过类似的问题。 一旦关闭相机控制器,您可以使用以下方式刷新视图旋转:
UIDeviceOrientation orientation = [[UIDevice currentDevice] orientation];
UIWindow *window = [[UIApplication sharedApplication] keyWindow];
UIView *topView = [window.subviews objectAtIndex:0];
[topView removeFromSuperview];
[window addSubview:topView];
因此,删除所显示窗口的最顶层视图并在窗口上重置它,它应该正确刷新视图旋转。
希望有所帮助
答案 3 :(得分:0)
也许,您可以尝试将其放在容器AVCaptureVideoPreviewLayer
中,而不是更改UIView
框架,并设置此容器的框架。 (我在ÀVCaptureVideoPreviewLayerthis way : sizing directly PreviewLayer had no effect, but sizing its containing
UIView上工作了一个'sizing'错误。
或者,
也许你可以在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];
}
祝你好运!
答案 4 :(得分:0)
由于不推荐使用interfaceOrientation,因此最新的swift 2.0解决方案应为:
let previewLayerConnection = self.previewLayer.connection
let orientation = AVCaptureVideoOrientation(rawValue: UIApplication.sharedApplication().statusBarOrientation.rawValue)
previewLayerConnection.videoOrientation = orientation!