2011-10-23 3 views
4

이 내 머리를하고있다가로 앱에서 화면에 비디오를 녹화하는 방법은 무엇입니까?

내가이 샘플 프로젝트에서 일하고있다 :. https://github.com/jj0b/AROverlayExample

이 완벽하게 작동합니다. 유일한 문제는 초상화 응용 프로그램입니다. 따라서 장치를 가로 방향으로 회전 시키면 비디오가 계속 표시되지만 내 레이블과 UI는 모두 옆으로 향하게됩니다.

- (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; 
} 
: 여기

enter image description here

내 코드입니다 :

enter image description here

문제는 이것이다 나는 단지의 Info.plist에 풍경을 설정하고있어이 문제를 해결하려면

이유를 알 수 없습니다. 비디오는 가로로 표시되며 특별히 가로 프레임 CGRectMake (0, 0, 480, 300)로 레이어 프레임을 설정 함에도 불구하고 다른 방향으로 나옵니다.

+0

이 AVcapturePreviewlayer이 스크린 샷에 기록? –

+0

은 화면 캡처 비디오에 기록 된 AVcapturePreviewlayer입니다. –

+0

나는 곤경에 처해있다! –

답변

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 
관련 문제