iOS获取本地视频和网络URL视频的缩略图方法

iOS获取本地视频和网络URL视频的缩略图方法

字数222 阅读612 评论0

首先大家先添加AVFoundation和CoreMedia.framework两个框架

NSString *path = @"www.51ios.net/本地路径"
MPMoviePlayerController *51iosMPMovie = [[MPMoviePlayerController alloc]
initWithContentURL:[NSURL fileURLWithPath:path]];
 UIImage *img = [51iosMPMovie thumbnailImageAtTime:0.0
timeOption:MPMovieTimeOptionNearestKeyFrame];

此处的img就是时间在0.0的缩略图
  • 第二种获取网络视频的缩略图
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就是网络视频的缩略图
  • 第三站方法用AVFoundation实现
+(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;
}
posted @ 2015-09-24 23:11  放逐之风  阅读(2830)  评论(0编辑  收藏  举报