AVAssetTrack的preferredTransform始终返回横向视角

7

我有一个记录视频的应用程序。

为了处理手机旋转,我有以下代码:

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

我有两个样本文件:一个是右横屏,一个是左横屏。我希望它们在代码中的朝向不同,但出乎意料的是它们的方向相同(而且videoAngleInDegree对它们都一样)。

是否有任何解决方法?

1个回答

2
你确定它正常工作吗?那里的 getVideoOrientation 看起来不对 - AVCapture 和 UIDevice 对于横向左右有相反的意义。
这里是一个正确的实现,来自于这个问题 - 检查那里的讨论并看看它是否有帮助。
- (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];
}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接