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 に答える