UIImagePicker中所选视频的方向

时间:2014-02-19 17:31:33

标签: 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];

1 个答案:

答案 0 :(得分:1)

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