2

我是 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];

}

4

2 回答 2

3

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

  1. 获得正确的方向
    更换

    if ([newCaptureVideoPreviewLayer isOrientationSupported]) {
        [newCaptureVideoPreviewLayer setOrientation:AVCaptureVideoOrientationPortrait];
    }  
    

    if ([newCaptureVideoPreviewLayer.connection isVideoOrientationSupported]) {  
        [newCaptureVideoPreviewLayer.connection setVideoOrientation:[UIDevice currentDevice].orientation];
    }
    
  2. 在初始化期间强制更新视频方向以通过 - (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;
    }
    
于 2013-08-05T10:44:29.767 回答
0

所以,这是一个解决方法,但我相信一定有比这更好的解决方案。我从这个问题中得到了部分代码:

iPhone App - 在横向模式下显示 AVFoundation 视频

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


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

[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];

}

于 2012-10-19T18:46:04.577 回答