2013-12-08 1 views
6

私はビデオを記録するアプリを持っています。携帯電話の回転を処理するためにAVAssetTrack preferredTransformは常にランドスケープを返します

私は次のコードを持っている:

// called on phone rotation 
    AVCaptureConnection *previewLayerConnection = [[self previewLayer] connection]; 
    if ([previewLayerConnection isVideoOrientationSupported]) { 
     [previewLayerConnection setVideoOrientation:[self getVideoOrientation]]; 
    } 

とgetVideoOrientation機能を:

- (AVCaptureVideoOrientation) getVideoOrientation { 
    UIInterfaceOrientation deviceOrientation = [[UIDevice currentDevice] orientation]; 
    AVCaptureVideoOrientation newOrientation = AVCaptureVideoOrientationPortrait; 
    switch (deviceOrientation) { 
     case UIInterfaceOrientationPortrait: 
      NSLog(@"UIInterfaceOrientationPortrait"); 
      newOrientation = AVCaptureVideoOrientationPortrait; 
      break; 
     case UIInterfaceOrientationLandscapeLeft: 
      NSLog(@"UIInterfaceOrientationLandscapeRight"); 
      newOrientation = AVCaptureVideoOrientationLandscapeLeft; 
      break; 
     case UIInterfaceOrientationLandscapeRight: 
      NSLog(@"UIInterfaceOrientationLandscapeLeft"); 
      newOrientation = AVCaptureVideoOrientationLandscapeRight; 
      break; 
     default: 
      NSLog(@"default"); 
      newOrientation = AVCaptureVideoOrientationPortrait; 
      break; 
    } 

    return newOrientation; 
} 

アプリのこの部分は、(私はすべてのデバイスの向き上で必要として、ビデオを参照してください正常に動作します)。 サムネイル(またはビデオを再生しようとするとき)に問題があります。

私はトラックごとに次の操作を行い、他の質問で読んだとおり:1 - 風景右、2 - 風景左:

 AVAssetTrack* videoTrack = [[asset tracksWithMediaType:AVMediaTypeVideo] objectAtIndex:0]; 
     CGAffineTransform txf  = [videoTrack preferredTransform]; 
     CGFloat videoAngleInDegree = RadiansToDegrees(atan2(txf.b, txf.a)); 

     if (txf.a == 0 && txf.b == 1.0 && txf.c == -1.0 && txf.d == 0) { 
      thumbOrientation = UIImageOrientationLeft; 
     } 
     if (txf.a == 0 && txf.b == -1.0 && txf.c == 1.0 && txf.d == 0) { 
      thumbOrientation = UIImageOrientationRight; 
     } 
     if (txf.a == 1.0 && txf.b == 0 && txf.c == 0 && txf.d == 1.0) { 
      thumbOrientation = UIImageOrientationUp; 
     } 
     if (txf.a == -1.0 && txf.b == 0 && txf.c == 0 && txf.d == -1.0) { 
      thumbOrientation = UIImageOrientationDown; 
     } 

     UIImage *image = [UIImage imageWithCGImage:im scale:1.0 orientation:thumbOrientation]; 

私は2つのサンプルファイルを持っています。上記のコードでは向きが異なることが予想されますが、予期せず同じものがあります(videoAngleInDegreeも同じです)。

回避策はありますか?

答えて

1

正常に動作していますか? getVideoOrientationは間違って見えます - AVCaptureとUIDeviceは左右の風景の意味が逆です。

正しい実装はthis questionです。ここでディスカッションをチェックし、役立つかどうかを確認してください。

- (void)deviceOrientationDidChange{ 

    UIDeviceOrientation deviceOrientation = [[UIDevice currentDevice] orientation]; 

    AVCaptureVideoOrientation newOrientation; 

    if (deviceOrientation == UIDeviceOrientationPortrait){ 
     NSLog(@"deviceOrientationDidChange - Portrait"); 
     newOrientation = AVCaptureVideoOrientationPortrait; 
    } 
    else if (deviceOrientation == UIDeviceOrientationPortraitUpsideDown){ 
     NSLog(@"deviceOrientationDidChange - UpsideDown"); 
     newOrientation = AVCaptureVideoOrientationPortraitUpsideDown; 
    } 

    // AVCapture and UIDevice have opposite meanings for landscape left and right (AVCapture orientation is the same as UIInterfaceOrientation) 
    else if (deviceOrientation == UIDeviceOrientationLandscapeLeft){ 
     NSLog(@"deviceOrientationDidChange - LandscapeLeft"); 
     newOrientation = AVCaptureVideoOrientationLandscapeRight; 
    } 
    else if (deviceOrientation == UIDeviceOrientationLandscapeRight){ 
     NSLog(@"deviceOrientationDidChange - LandscapeRight"); 
     newOrientation = AVCaptureVideoOrientationLandscapeLeft; 
    } 

    else if (deviceOrientation == UIDeviceOrientationUnknown){ 
     NSLog(@"deviceOrientationDidChange - Unknown "); 
     newOrientation = AVCaptureVideoOrientationPortrait; 
    } 

    else{ 
     NSLog(@"deviceOrientationDidChange - Face Up or Down"); 
     newOrientation = AVCaptureVideoOrientationPortrait; 
    } 

    [self setOrientation:newOrientation]; 
} 
関連する問題