2013-12-08 17 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; 
} 

應用的這部分工作正常(我看到視頻,我應該在任何設備方向)。 但是,當我嘗試製作縮略圖(或播放視頻)時,我遇到了問題。

正如我在我做的每一曲目以下的其他問題已經閱讀:

 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個樣本文件:1 - 景觀權,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]; 
}