我是iOS的新手,尝试使用AVCAM创建自定义相机。我在获得景观方向的预览很难 - 它使视图顺时针旋转90度并在半屏幕上显示。
我收到此消息 -
警告: - [setorientation:]不推荐使用。
请使用avcaptureconnection的-setVideoInientation:
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];
}
上次我也偶然发现了这个问题。我通过做两件事解决了这个问题
-
获得正确的方向
更换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; }
所以,这是一个解决方法,但是我相信必须有比这更好的解决方案。我从这个问题中获得了代码的一部分:
iPhone应用程序 - 在景观模式上显示Avfoundation视频
,但必须针对每个方向调整框架才能使其在ios6上工作(并且仍然显示警告):
- (void)willanimatimatotation tointerfaceorientation:(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];
}