xcode 获取缩略图的视频快照

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9145968/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 23:12:50  来源:igfitidea点击:

Getting video snapshot for thumbnail

iosxcodevideoavfoundation

提问by random

I am recording a video from the iPhone camera by using the AVCam code provided from apple.

我正在使用 Apple 提供的 AVCam 代码从 iPhone 相机录制视频。

After the video is recorded it is saved to the photos library.

录制视频后,它会保存到照片库中。

A new view is then loaded, here I need to have an image thumbnail of the video.

然后加载一个新视图,这里我需要视频的图像缩略图。

I have a path to the video:

我有一个视频路径:

file://localhost/private/var/mobile/Applications/ED45DEFC-ABF9-4A5E-9102-21680CC1448E/tmp/output.mov

I can't seem to figure how to get the first frame of the video to use as a thumbnail.

我似乎无法弄清楚如何将视频的第一帧用作缩略图。

Any help would be very appreciated and thank you for your time.

任何帮助将不胜感激,并感谢您的时间。



EDIT

编辑

I ended up using this, I'm not sure why it returns the image sideways?

我最终使用了这个,我不知道为什么它会横向返回图像?

- (UIImage*)loadImage {

    AVURLAsset *asset = [[AVURLAsset alloc] initWithURL:vidURL options:nil];
    AVAssetImageGenerator *generate = [[AVAssetImageGenerator alloc] initWithAsset:asset];
    NSError *err = NULL;
    CMTime time = CMTimeMake(1, 60);
    CGImageRef imgRef = [generate copyCGImageAtTime:time actualTime:NULL error:&err];
    NSLog(@"err==%@, imageRef==%@", err, imgRef);

    return [[UIImage alloc] initWithCGImage:imgRef];

}

回答by djromero

To fixthe thumbnail orientation set appliesPreferredTrackTransformto YESin the AVAssetImageGeneratorinstance. If you add your own video composition, you'll need to include the right transform to rotate the video as wanted.

为了解决缩略图方向设置appliesPreferredTrackTransform,以YESAVAssetImageGenerator实例。如果您添加自己的视频合成,则需要包含正确的变换以根据需要旋转视频。

generate.appliesPreferredTrackTransform = YES;

Remember to release the obtained image reference with CGImageRelease.

记得用 释放获得的图像参考CGImageRelease

To request multiple thumbnails it's better to do asynchronously with generateCGImagesAsynchronouslyForTimes:completionHandler:.

要请求多个缩略图,最好与generateCGImagesAsynchronouslyForTimes:completionHandler:.