Created
February 25, 2019 07:18
-
-
Save dhl613/7cf36776631aff2d63ea0c97a76ea48a to your computer and use it in GitHub Desktop.
获取视频缩略图
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/// 方法一: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