首先大家先添加AVFoundation和CoreMedia.framework兩個(gè)框架
- 第一種本地視頻獲取縮略圖
NSString *path = @"www.51ios.net/本地路徑"
MPMoviePlayerController *51iosMPMovie = [[MPMoviePlayerController alloc]
initWithContentURL:[NSURL fileURLWithPath:path]];
UIImage *img = [51iosMPMovie thumbnailImageAtTime:0.0
timeOption:MPMovieTimeOptionNearestKeyFrame];
此處的img就是時(shí)間在0.0的縮略圖
- 第二種獲取網(wǎng)絡(luò)視頻的縮略圖
NSString *videoURL = @"http://www.51ios.net/archives/784"
MPMoviePlayerController *51iosMPMovie = [[MPMoviePlayerController alloc]initWithContentURL:videoURL]; 51iosMPMovie.shouldAutoplay = NO;
UIImage *thumbnail = [51iosMPMovie thumbnailImageAtTime:time timeOption:MPMovieTimeOptionNearestKeyFrame];
此處的thumbnail就是網(wǎng)絡(luò)視頻的縮略圖
- 第三站方法用AVFoundation實(shí)現(xiàn)
+(UIImage *)getThumbnailImage:(NSString *)videoURL
{
AVURLAsset *asset = [[AVURLAsset alloc] initWithURL:[NSURL fileURLWithPath:videoURL] options:nil];
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;
}
注:文章轉(zhuǎn)載自博客