ios - UIImagePicker 中所选视频的方向

标签 ios objective-c video orientation uiimagepickercontroller

如何获取从 UIImagepickerController 中的图库中选择的视频的方向?

UIImagePickerController * videoRecorder = [[UIImagePickerController alloc] init];

videoRecorder.delegate = self;
videoRecorder.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
videoRecorder.mediaTypes = [[NSArray alloc] initWithObjects:(NSString *)kUTTypeMovie,      nil];
 [self presentViewController:videoRecorder animated:YES completion:nil];

最佳答案

ALAssetsLibrary 获取您的 AVAsset 并使用以下类别:

@interface ALAssetsLibrary (VideoOrientation)

+ (UIInterfaceOrientation)orientationForTrack:(AVAsset *)asset;

@end


@implementation ALAssetsLibrary (VideoOrientation)


+ (UIInterfaceOrientation)orientationForTrack:(AVAsset *)asset
{
    NSArray *videoAssets = [asset tracksWithMediaType:AVMediaTypeVideo];

    if (!videoAssets.count)
    {
        NSLog(@"No video assets found.");
        return UIInterfaceOrientationPortrait;
    }

    // Get the video track.
    AVAssetTrack *videoTrack = [videoAssets objectAtIndex:0];

    CGSize size = [videoTrack naturalSize];
    CGAffineTransform preferredTransform = [videoTrack preferredTransform];

    // Return interface orientation based on
    // the preferred transform and size of the video.
    if (size.width == preferredTransform.tx &&
        size.height == preferredTransform.ty)
    {
        return UIInterfaceOrientationLandscapeRight;
    }
    else if (preferredTransform.tx == 0 &&
             preferredTransform.ty == 0)
    {
        return UIInterfaceOrientationLandscapeLeft;
    }
    else if (preferredTransform.tx == 0 &&
             preferredTransform.ty == size.width)
    {
        return UIInterfaceOrientationPortraitUpsideDown;
    }
    else
    {
        return UIInterfaceOrientationPortrait;
    }
}

@end

关于ios - UIImagePicker 中所选视频的方向,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21887970/

相关文章:

ios - 当数据在两个字典中时,如何区分数据和json?

ios - AVPlayerItem canStepBackward/canPlayReverse 条件

c++ - Tee/passthrough DirectShow 数据作为视频源

video - 如何在网站上提供不同格式的视频流选项

iOS - 查找 View 的最高约束?

ios - 删除 tabBarController 中的所有对象,然后恢复默认的 tabBar?

ios - 请求相机权限时 requestAccessForMediaType 的响应崩溃

iphone - 在iPhone上使用sqlite3_create_collat​​ion

iphone - UITableView 的屏幕截图

azure - 使用 ffmpeg 将实时内容流式传输到 azure 媒体服务