Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/116.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 从使用AVAssetImageGenerator获得的视频或拇指获取方向_Ios_Iphone_Video_Avasset_Avassetimagegenerator - Fatal编程技术网

Ios 从使用AVAssetImageGenerator获得的视频或拇指获取方向

Ios 从使用AVAssetImageGenerator获得的视频或拇指获取方向,ios,iphone,video,avasset,avassetimagegenerator,Ios,Iphone,Video,Avasset,Avassetimagegenerator,在我的应用程序中,我从单个图像创建视频。一切正常,视频正确组装,大小和方向正确。它们在Apple photo应用程序、MPMoviePlayer和我保存它们的沙盒目录中都正确显示 当我试图从电影中得到一个拇指时,问题就出现了。方向不正确,我不知道如何修复,我看到有一个-preferredTransform属性,但是横向和纵向视频的结果都是一样的我使用的url是沙盒目录路径。 以下是片段: - (void) setVideoPath:(NSString *)videoPath { if (

在我的应用程序中,我从单个图像创建视频。一切正常,视频正确组装,大小和方向正确。它们在Apple photo应用程序、MPMoviePlayer和我保存它们的沙盒目录中都正确显示
当我试图从电影中得到一个拇指时,问题就出现了。方向不正确,我不知道如何修复,我看到有一个-
preferredTransform
属性,但是横向和纵向视频的结果都是一样的
我使用的url是沙盒目录路径。 以下是片段:

- (void) setVideoPath:(NSString *)videoPath {
    if (videoPath ==_videoPath) {
        return;
    }
    _videoPath = videoPath;
    AVAsset *asset = [AVAsset assetWithURL:[NSURL fileURLWithPath:_videoPath]];
    AVAssetImageGenerator *imageGenerator = [[AVAssetImageGenerator alloc]initWithAsset:asset];
    CMTime time = CMTimeMake(1, 1);
    CGImageRef imageRef = [imageGenerator copyCGImageAtTime:time actualTime:NULL error:NULL];
    UIImage *thumbnail = [UIImage imageWithCGImage:imageRef];
    CGImageRelease(imageRef);  
    self.videoImageView.image = thumbnail;
}

现在我找到了一个技巧,找到视频旋转并应用到缩略图。这似乎可行,但我很确定还有其他办法。 解决方案就是从这个角度出发,归功于卢卡·贝尔纳迪

{
//..
        NSArray *videoTracks = [asset tracksWithMediaType:AVMediaTypeVideo];
        AVAssetTrack* videoTrack    = [videoTracks firstObject];
        CGAffineTransform txf       = [videoTrack preferredTransform];        
        if (txf.a == 0 && txf.b == 1.0 && txf.c == -1.0 && txf.d == 0) {
            thumbOrientation = UIImageOrientationRight;
        }
        if (txf.a == 0 && txf.b == -1.0 && txf.c == 1.0 && txf.d == 0) {
            thumbOrientation =  UIImageOrientationLeft;
        }
        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 *thumbnail = [UIImage imageWithCGImage:imageRef scale:[[UIScreen mainScreen]scale] orientation:thumbOrientation];
//..
}

只需使用这个,无需编写任何方法

generator.appliesPreferredTrackTransform=true

其中url是视频url。

SWIFT版本

            // generate thumb from video and set it
        var err: NSError? = nil
        let asset = AVURLAsset(URL: NSURL(fileURLWithPath: videoPath! as String), options: nil)
        let imgGenerator = AVAssetImageGenerator(asset: asset)
        imgGenerator.appliesPreferredTrackTransform = true
        let cgImage: CGImage!
        do {
            cgImage = try imgGenerator.copyCGImageAtTime(CMTimeMake(0, 60), actualTime: nil)
            let thumbnail = UIImage(CGImage: cgImage)
        } catch let error as NSError {
            err = error
            cgImage = nil
        }
最重要的方向线:

imgGenerator.appliesPreferredTrackTransform = true

太神了谢谢
imgGenerator.appliesPreferredTrackTransform = true