7

在我的应用程序中,我从单个图像创建视频。一切正常,视频以正确的大小和方向正确组装。它们在 Apple 照片应用程序、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;
}
4

4 回答 4

41

就用这个,不需要写任何方法

generator.appliesPreferredTrackTransform=true
于 2014-04-05T08:16:46.580 回答
3

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
于 2016-05-26T09:43:31.170 回答
0

目前我找到了一个技巧,找到视频旋转并应用于缩略图。它似乎有效,但我很确定还有另一种方法。解决方案取自这个要点,归功于 Luca Bernardi。

{
//..
        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];
//..
}
于 2013-12-04T06:47:06.147 回答
0
-(UIImage *)generateThumbImage : (NSURL *)url
{
     AVAsset *asset = [AVAsset assetWithURL:url];
     AVAssetImageGenerator *imageGenerator = [[AVAssetImageGenerator alloc]initWithAsset:asset];
     imageGenerator.appliesPreferredTrackTransform = YES;
     CMTime time = [asset duration];
     time.value = 0;
     CGImageRef imageRef = [imageGenerator copyCGImageAtTime:time actualTime:NULL error:NULL];
     UIImage *thumbnail = [UIImage imageWithCGImage:imageRef];
     CGImageRelease(imageRef);  // CGImageRef won't be released by ARC
     return thumbnail;
}

其中url是视频 url。

于 2015-11-21T07:52:28.017 回答