2016-08-17 13 views
0

AVCaptureSessionを使用してビデオを録画するときに、ビデオの向きを変更する必要があります。次のコードを使用して録音を開始する前に向きを変更するようにしましたが、録音処理中に向きを変更する必要があります。AVCapturesessionは、録画中にビデオの向きを変更します。

- (void)viewWillTransitionToSize:(CGSize)size withTransitionCoordinator:(id<UIViewControllerTransitionCoordinator>)coordinator { 
    [self orientationChanged]; 
} 

//Respond with the rotation 
- (void)orientationChanged { 
    AVCaptureConnection *videoConnection = nil; 

    for (AVCaptureConnection *connection in [_movieFileOutput connections]) 
    { 
     NSLog(@"%@", connection); 
     for (AVCaptureInputPort *port in [connection inputPorts]) 
     { 
      NSLog(@"%@", port); 
      if ([[port mediaType] isEqual:AVMediaTypeVideo]) 
      { 
       videoConnection = connection; 
       break; 
      } 
     } 
    } 

    UIDeviceOrientation deviceOrientation = [UIDevice currentDevice].orientation; 
    if (deviceOrientation == UIInterfaceOrientationPortraitUpsideDown) { 
     [_previewLayer.connection setVideoOrientation:AVCaptureVideoOrientationPortraitUpsideDown]; 
     [videoConnection setVideoOrientation:AVCaptureVideoOrientationPortraitUpsideDown]; 
    } 
    else if (deviceOrientation == UIInterfaceOrientationPortrait) { 
     [_previewLayer.connection setVideoOrientation:AVCaptureVideoOrientationPortrait]; 
     [videoConnection setVideoOrientation:AVCaptureVideoOrientationPortrait]; 
    } 

    else if (deviceOrientation == UIInterfaceOrientationLandscapeLeft) { 
     [_previewLayer.connection setVideoOrientation:AVCaptureVideoOrientationLandscapeLeft]; 
     [videoConnection setVideoOrientation:AVCaptureVideoOrientationLandscapeLeft]; 
    } 

    else { 
     [_previewLayer.connection setVideoOrientation:AVCaptureVideoOrientationLandscapeRight]; 
     [videoConnection setVideoOrientation:AVCaptureVideoOrientationLandscapeRight]; 
    } 
} 

答えて

0

あなたは、レコードの処理中に方向を変更するには、このコードを使用することができます:

AVCaptureConnection *videoConnection = [self.videoOutPut connectionWithMediaType:AVMediaTypeVideo]; 
- (void)_setOrientationForConnection:(AVCaptureConnection *)connection 
{ 
    if (!connection || ![connection isVideoOrientationSupported]) 
     return; 

    AVCaptureVideoOrientation captureOrientation = AVCaptureVideoOrientationPortrait; 

    UIDeviceOrientation deviceOrientation = [UIDevice currentDevice].orientation; 
    switch (deviceOrientation) { 
     case UIDeviceOrientationLandscapeLeft: 
      captureOrientation = AVCaptureVideoOrientationLandscapeRight; 
      break; 
     case UIDeviceOrientationLandscapeRight: 
      captureOrientation = AVCaptureVideoOrientationLandscapeLeft; 
      break; 
     case UIDeviceOrientationPortrait: 
      captureOrientation = AVCaptureVideoOrientationPortrait; 
      break; 
     case UIDeviceOrientationPortraitUpsideDown: 
      captureOrientation = AVCaptureVideoOrientationPortraitUpsideDown; 
      break; 
     default: 
      break; 
    } 

    [connection setVideoOrientation:captureOrientation]; 
} 
関連する問題