Skip to content

Instantly share code, notes, and snippets.

@dhl613
Created February 25, 2019 07:18
Show Gist options
  • Save dhl613/7cf36776631aff2d63ea0c97a76ea48a to your computer and use it in GitHub Desktop.
Save dhl613/7cf36776631aff2d63ea0c97a76ea48a to your computer and use it in GitHub Desktop.
获取视频缩略图
/// 方法一:MPMoviePlayerController
NSString *videoURL = @"http:// or /private(local video)"
NSURL *URL = [NSURL URLWithString: videoURL];
// NSURL *URL = [NSURL fileURLWithPath:videoURL]
MPMoviePlayerController *iosMPMovie = [[MPMoviePlayerController alloc]initWithContentURL:URL]; iosMPMovie.shouldAutoplay = NO;
UIImage *thumbnail = [iosMPMovie thumbnailImageAtTime:time timeOption:MPMovieTimeOptionNearestKeyFrame];
/// 方法二:AVAssetImageGenerator
+ (UIImage *)getThumbnailImage:(NSString *)videoURL {
// 本地视频
AVURLAsset *asset = [[AVURLAsset alloc] initWithURL:[NSURL fileURLWithPath:videoURL] options:nil];
// 网络视频
// AVURLAsset *asset = [AVURLAsset assetWithURL:[NSURL URLWithString: videoURL]];
AVAssetImageGenerator *gen = [[AVAssetImageGenerator alloc] initWithAsset:asset];
gen.appliesPreferredTrackTransform = YES;
CMTime time = CMTimeMakeWithSeconds(0.0, 600);
NSError *error = nil;
CMTime actualTime;
CGImageRef image = [gen copyCGImageAtTime:time actualTime:&actualTime error:&error];
UIImage *thumb = [[UIImage alloc] initWithCGImage:image];
CGImageRelease(image);
return thumb;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment