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?
source share