首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >AvCam iOS 6的景观

AvCam iOS 6的景观
EN

Stack Overflow用户
提问于 2012-10-19 01:35:59
回答 2查看 3.3K关注 0票数 2

我是iOS的新手,尝试用AvCam创建定制相机。我有困难获得景观方向预览-它旋转90度顺时针,并显示在半屏幕上。

我收到这条消息--

警告:- setOrientation:是不推荐的。

请使用AVCaptureConnection的-setVideoOrientation:

AVCaptureConnection已经设定了方向,所以我不知道我还能做什么。

我知道在iOS的早期版本(4,5)中,这个问题被问了很多次,但是这些技术/代码对我没有用(iOS 6)。

原始代码(没有对Apple做任何更改)

代码语言:javascript
复制
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块:

代码语言:javascript
复制
-(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];

}

EN

回答 2

Stack Overflow用户

发布于 2013-08-05 10:44:29

上次我也偶然发现了这个问题。我做了两件事解决了这个问题

  1. 找到正确的方向 Replaceif (newCaptureVideoPreviewLayer isOrientationSupported) { newCaptureVideoPreviewLayer isOrientationSupported }Withif (newCaptureVideoPreviewLayer.connection isVideoOrientationSupported) { [newCaptureVideoPreviewLayer.connection setVideoOrientation:UIDevice currentDevice.orientation];}
  2. 通过在- (void)deviceOrientationDidChange中手动触发AVCaptureManager.m,强制在初始化过程中更新视频方向以捕获景观模式下的视频输出。 我把它加到:

  • ( BOOL ) setupSession {BOOL成功=否;performSelector:@selector(deviceOrientationDidChange);AVCamRecorder *AVCamRecorder *newRecorder = [AVCamRecorder alloc initWithSession:self session outputFileURL:self.lastOutputfileURL];newRecorder setDelegate: self;self AVCamRecorder.返回成功;}
票数 3
EN

Stack Overflow用户

发布于 2012-10-19 18:46:04

因此,这里有一个解决办法,但我相信肯定会有比这更好的解决办法。我从这个问题中得到了一部分代码:

iPhone App - Show AVFoundation video on landscape mode

但是,为了使其在iOS6上工作(它仍然显示警告),必须为每个方向调整框架:

-(void)willAnimateRotationToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation持续时间:(NSTimeInterval)持续时间{

代码语言:javascript
复制
[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];

}

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/12966175

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档