AvCam iOS 6 中的横向
Posted
技术标签:
【中文标题】AvCam iOS 6 中的横向【英文标题】:Landscape in AvCam iOS 6 【发布时间】:2012-10-19 01:35:59 【问题描述】:我是 ios 新手,正在尝试使用 AvCam 创建自定义相机。我无法获得横向预览 - 它会将视图顺时针旋转 90 度并在半屏上显示。
我收到了这条消息——
警告:-[ setOrientation:] 已弃用。
请使用 AVCaptureConnection 的 -setVideoOrientation:
AVCaptureConnection 已经设置了方向,所以我不知道我应该怎么做。
我知道以前版本的 iOS (4,5) 曾多次问过这个问题,但这些技术/代码都不适用于我 (iOS 6)。
原始代码(Apple 未做任何更改)
if ([self captureManager] == nil)
AVCamCaptureManager *manager = [[AVCamCaptureManager alloc] init];
[self setCaptureManager:manager];
[manager release];
[[self captureManager] setDelegate:self];
if ([[self captureManager] setupSession])
// Create video preview layer and add it to the UI
AVCaptureVideoPreviewLayer *newCaptureVideoPreviewLayer = [[AVCaptureVideoPreviewLayer alloc] initWithSession:[[self captureManager] session]];
UIView *view = [self videoPreviewView];
CALayer *viewLayer = [view layer];
[viewLayer setMasksToBounds:YES];
CGRect bounds = [view bounds];
[newCaptureVideoPreviewLayer setFrame:bounds];
if ([newCaptureVideoPreviewLayer isOrientationSupported])
[newCaptureVideoPreviewLayer setOrientation:AVCaptureVideoOrientationPortrait];
[newCaptureVideoPreviewLayer setVideoGravity:AVLayerVideoGravityResizeAspectFill];
[viewLayer insertSublayer:newCaptureVideoPreviewLayer below:[[viewLayer sublayers] objectAtIndex:0]];
[self setCaptureVideoPreviewLayer:newCaptureVideoPreviewLayer];
AVCaptureConnection 块:
-(void)startRecordingWithOrientation:(AVCaptureVideoOrientation)videoOrientation;
AVCaptureConnection *videoConnection = [AVCamUtilities connectionWithMediaType:AVMediaTypeVideo fromConnections:[[self movieFileOutput] connections]];
if ([videoConnection isVideoOrientationSupported])
[videoConnection setVideoOrientation:videoOrientation];
[[self movieFileOutput] startRecordingToOutputFileURL:[self outputFileURL] recordingDelegate:self];
【问题讨论】:
【参考方案1】:上次我也偶然发现了这个问题。我通过做两件事解决了这个问题
获得正确的方向 替换
if ([newCaptureVideoPreviewLayer isOrientationSupported])
[newCaptureVideoPreviewLayer setOrientation:AVCaptureVideoOrientationPortrait];
与
if ([newCaptureVideoPreviewLayer.connection isVideoOrientationSupported])
[newCaptureVideoPreviewLayer.connection setVideoOrientation:[UIDevice currentDevice].orientation];
通过触发在初始化期间强制更新视频方向以捕获横向模式下的视频输出
- (void)deviceOrientationDidChange
内手动AVCaptureManager.m
我添加到:
- (BOOL) setupSession
BOOL success = NO;
...
AVCamRecorder *newRecorder = [[AVCamRecorder alloc] initWithSession:[self session] outputFileURL:self.lastOutputfileURL];
[newRecorder setDelegate:self];
[self performSelector:@selector(deviceOrientationDidChange)];
...
return success;
【讨论】:
【参考方案2】:所以,这里有一个解决方法,但我确信一定有比这更好的解决方案。我从这个问题中得到了部分代码:
iPhone App - Show AVFoundation video on landscape mode
但必须为每个方向调整框架以使其在 iOS6 上工作(它仍然显示警告):
-(void)willAnimateRotationToInterfaceOrientation: (UIInterfaceOrientation)到InterfaceOrientation 持续时间:(NSTimeInterval)持续时间
[CATransaction begin];
if (toInterfaceOrientation==UIInterfaceOrientationLandscapeLeft)
captureVideoPreviewLayer.orientation = UIInterfaceOrientationLandscapeLeft;
captureVideoPreviewLayer.frame = CGRectMake(0, 0, 480, 320);
else if (toInterfaceOrientation==UIInterfaceOrientationPortrait)
captureVideoPreviewLayer.orientation = UIInterfaceOrientationPortrait;
captureVideoPreviewLayer.frame = CGRectMake(0, 0, 320, 480);
else if (toInterfaceOrientation==UIInterfaceOrientationLandscapeRight)
captureVideoPreviewLayer.orientation = UIInterfaceOrientationLandscapeRight;
captureVideoPreviewLayer.frame = CGRectMake(0, 0, 480, 320);
[CATransaction commit];
[super willAnimateRotationToInterfaceOrientation:toInterfaceOrientation duration:duration];
【讨论】:
本帖有解决方向警告的办法——***.com/questions/11532337/…以上是关于AvCam iOS 6 中的横向的主要内容,如果未能解决你的问题,请参考以下文章
iOS 6 Game Center 在横向模式 cocos2d 中的身份验证崩溃
ios 6 中的 UITabBarController 旋转问题