4

这让我很头疼。

我正在使用这个示例项目:https ://github.com/jj0b/AROverlayExample

这完美地工作。唯一的问题是它是一个肖像应用程序。因此,当我将设备旋转到横向时,视频仍会按需要显示,但我的所有标签和 UI 现在都是横向的。

在此处输入图像描述

为了解决这个问题,我只在 info.plist 中设置风景

问题是这样的:

在此处输入图像描述

这是我的代码:

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];

    if (self) 
    {
        self.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;

        assert( self.autoresizesSubviews );


        CGPoint layerRectCenter = CGPointMake( CGRectGetMidX( frame ),
                                              CGRectGetMidY( frame ) );


        // Initialization code
        self.captureSession = [[[AVCaptureSession alloc] init] autorelease];

        // 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");
        }

        // addVideoPreviewLayer 
        {
            self.previewLayer = [[[AVCaptureVideoPreviewLayer alloc] initWithSession: self.captureSession] autorelease];
            self.previewLayer.videoGravity = AVLayerVideoGravityResizeAspectFill;

            self.previewLayer.frame = CGRectMake(0, 0, 480, 300); 
            self.previewLayer.orientation = AVCaptureVideoOrientationLandscapeRight;


            //self.previewLayer.frame = frame;

            [self.layer addSublayer: self.previewLayer];
        }

        // run!
        [self.captureSession startRunning];

    }
    return self;
}

我无法弄清楚为什么视频会横向显示,尽管专门将图层框架设置为横向 CGRectMake(0, 0, 480, 300) ,但它却以相反的方式出现。

4

1 回答 1

2

一个非常巧妙的解决方案是将视图直接添加到窗口中。

这样做的最佳点是在根视图控制器中:

// need to add the capture view to the window here: can't do it in viewDidLoad as the window is not ready at that point
- (void) viewDidAppear: (BOOL) animated
{
    self.frontCamView = [[[FrontCamView alloc] 
                          initWithFrame: [UIScreen mainScreen].bounds] 
                         autorelease];

    self.view.opaque = NO;
    self.view.backgroundColor = [UIColor clearColor];

    [self.view.window addSubview: self.frontCamView];
    [self.view.window sendSubviewToBack: self.frontCamView];
}

然后是相机视图本身的实现:

@interface FrontCamView ( )

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

@end

// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

@implementation FrontCamView

@synthesize captureSession;
@synthesize previewLayer;

// - - - 

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];

    if (self) 
    {
        self.captureSession = [[[AVCaptureSession alloc] init] autorelease];

        // 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");
        }

        // addVideoPreviewLayer 
        {
            CGRect screenRect = [UIScreen mainScreen].bounds;

            self.previewLayer = [[[AVCaptureVideoPreviewLayer alloc] initWithSession: self.captureSession] autorelease];

            self.previewLayer.videoGravity = AVLayerVideoGravityResizeAspectFill;
            self.previewLayer.frame = screenRect;  
            self.previewLayer.orientation = AVCaptureVideoOrientationPortrait;         

            [self.layer addSublayer: self.previewLayer];

        }

        // run!
        [self.captureSession startRunning];

    }
    return self;
}

- (void) dealloc 
{
    [self.captureSession stopRunning];

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


    [super dealloc];
}

@end
于 2011-10-24T16:09:20.323 回答