在AVCaptureVideoPreviewLayer中使用AVFoundation实时捕获旋转90º

时间:2015-04-19 00:42:30

标签: ios cocoa-touch video rotation avfoundation

我正在制作一个应用程序,允许用户实时录制视频,同时在预览图层中显示一些HUD信息。

我已经解决了很多问题,但我遇到的一个问题是,当iPad处于横向模式时,视频本身会向右旋转90度。当主页按钮位于左侧时,视频将逆时针旋转90度,而当主页按钮位于右侧时,视频会旋转90度。无论我使用哪种横向模式,视频都应该是直立的。这个[SO消息]似乎与我正在处理的内容非常接近,但解决方案包含了弃用。 1

在我的主控制器中:

[self setCaptureManager:[[[CaptureSessionManager alloc] init] autorelease]];

[[self captureManager] addVideoInput];

[[self captureManager] addVideoPreviewLayer];
CGRect layerRect = [[[self view] layer] bounds];
[[[self captureManager] previewLayer] setBounds:layerRect];
[[[self captureManager] previewLayer] setPosition:CGPointMake(CGRectGetMidX(layerRect),
        CGRectGetMidY(layerRect))];
[[[self view] layer] addSublayer:[[self captureManager] previewLayer]];

CaptureSessionManager.h

#import <CoreMedia/CoreMedia.h>
#import <AVFoundation/AVFoundation.h>


@interface CaptureSessionManager : NSObject {

}

@property(retain) AVCaptureVideoPreviewLayer *previewLayer;
@property(retain) AVCaptureSession *captureSession;

- (void)addVideoPreviewLayer;

- (void)addVideoInput;

@end

CaptureSessionManager.m

#import "CaptureSessionManager.h"


@implementation CaptureSessionManager

@synthesize captureSession;
@synthesize previewLayer;

#pragma mark Capture Session Configuration

- (id)init {
    if ((self = [super init])) {
        [self setCaptureSession:[[AVCaptureSession alloc] init]];
        if ([captureSession canSetSessionPreset:AVCaptureSessionPresetHigh]) {
            captureSession.sessionPreset = AVCaptureSessionPresetHigh;
        }
    }
    return self;
}

- (void)addVideoPreviewLayer {
    [self setPreviewLayer:[[[AVCaptureVideoPreviewLayer alloc] initWithSession:[self captureSession]] autorelease]];
    [[self previewLayer] setVideoGravity:AVLayerVideoGravityResizeAspectFill];

}

- (void)addVideoInput {
    AVCaptureDevice *videoDevice = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo];
    if (videoDevice) {
        NSError *error;
        AVCaptureDeviceInput *videoIn = [AVCaptureDeviceInput deviceInputWithDevice:videoDevice error:&error];
        if (!error) {
            if ([[self captureSession] canAddInput:videoIn])
                [[self captureSession] addInput:videoIn];
            else
                NSLog(@"Couldn't add video input");
        }
        else
            NSLog(@"Couldn't create video input");
    }
    else
        NSLog(@"Couldn't create video capture device");
}

- (void)dealloc {

    [[self captureSession] stopRunning];

    [previewLayer release], previewLayer = nil;
    [captureSession release], captureSession = nil;

    [super dealloc];
}

@end

2 个答案:

答案 0 :(得分:1)

如果您提到的那个问题的答案不起作用,那么您可以通过其他方式实现。使用CATransform3DMakeRotation根据设备的方向旋转预览图层。

例如,对于UIDeviceOrientationLandscapeLeft,您可以像这样旋转预览图层:

preview.transform = CATransform3DMakeRotation(-M_PI/2, 0, 0, 1);

答案 1 :(得分:1)

发现需要设置预览图层连接的videoOrientation属性。

以下是我的addVideoPreviewLayer方法的更改......

- (void)addVideoPreviewLayer {
    [self setPreviewLayer:[[[AVCaptureVideoPreviewLayer alloc] initWithSession:[self captureSession]] autorelease]];
    [[self previewLayer] setVideoGravity:AVLayerVideoGravityResizeAspectFill];
    [[previewLayer connection] setVideoOrientation:AVCaptureVideoOrientationLandscapeRight];
}