我正在使用最新的SDK开发iOS应用程序。
我正在开发一款仅支持LandscapeRight方向的相机应用程序。
在主要ViewController
上我为UIView
添加了AVCaptureVideoPreviewLayer
。换句话说,主ViewController
我有主视图,另一个视图videoPreviewView
,AVCaptureVideoPreviewLayer
。
我在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}}
但是UIInterfaceOrientation
是UIInterfaceOrientationLandscapeRight
,我会得到这些值:
BOUNDS: {{0, 0}, {480, 320}}
另一个问题是我看到视频旋转-90度。
我找到了这个stackoverflow question,但永远不会触发- (void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation
方法。
发生了什么事?我怎样才能解决这个问题?
答案 0 :(得分:-1)
iOS6的:
AVCaptureConnection *previewLayerConnection=self.newCaptureVideoPreviewLayer.connection;
if ([previewLayerConnection isVideoOrientationSupported])
[previewLayerConnection setVideoOrientation:AVCaptureVideoOrientationLandscapeRight];
< iOS6的:
if ([self.newCaptureVideoPreviewLayer isOrientationSupported])
[self.newCaptureVideoPreviewLayer setOrientation:AVCaptureVideoOrientationLandscapeRight];