AVAssetTrack preferredTransform always returns landscape

I have an application that records video.

To handle the rotation of the phone, I have the following code:

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

and getVideoOrientation function:

 - (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; } 

This part of the application works correctly (I see the video as I should on any orientation of the device). But when I try to make a thumbnail (or play a video), I have problems.

As I read in other questions, I do the following for each track:

  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]; 

I have 2 sample files: 1 - landscape on the right, 2 - landscape on the left. I expect that they have different orientations in the code above, but unexpectedly they have the same ones (and videoAngleInDegree is the same for both).

Are there any workarounds?

+6
source share
1 answer

Are you sure it is working correctly? getVideoOrientation does not look right - AVCapture and UIDevice have opposite values ​​for the landscape on the left and right.

Here is the correct implementation from this question - check out the discussion and see if this helps.

 - (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]; } 
+1
source

All Articles