【1】 获取本地视频的缩略图
+(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;
}
第二种方法
需要添加AVFoundation和CoreMedia.framework
另外一种那个方法
MPMoviePlayerController *moviePlayer = [[MPMoviePlayerController alloc]initWithContentURL:videoURL]; moviePlayer.shouldAutoplay = NO;
UIImage *thumbnail = [moviePlayer thumbnailImageAtTime:time timeOption:MPMovieTimeOptionNearestKeyFrame];
//这个也一样
+(UIImage *)fFirstVideoFrame:(NSString *)path
{
MPMoviePlayerController *mp = [[MPMoviePlayerController alloc]
initWithContentURL:[NSURL fileURLWithPath:path]];
UIImage *img = [mp thumbnailImageAtTime:0.0
timeOption:MPMovieTimeOptionNearestKeyFrame];
[mp stop];
[mp release];
return img;
}
+ (UIImage *)getImageWihtUrl:(NSString *)videoURL
{
NSDictionary *opts = [NSDictionary dictionaryWithObject:[NSNumber numberWithBool:NO] forKey:AVURLAssetPreferPreciseDurationAndTimingKey];
// NSURL *url = [[[NSURL alloc] initFileURLWithPath:videoURL] autorelease];
NSURL *url=[[NSURL alloc]initWithString:videoURL];
AVURLAsset *urlAsset = [AVURLAsset URLAssetWithURL:url options:opts];
AVAssetImageGenerator *generator = [AVAssetImageGenerator assetImageGeneratorWithAsset:urlAsset];
generator.appliesPreferredTrackTransform = YES;
generator.maximumSize = CGSizeMake(240, 150);
NSError *error = nil;
CGImageRef img = [generator copyCGImageAtTime:CMTimeMake(60, 10) actualTime:NULL error:&error];
UIImage *image = [UIImage imageWithCGImage: img];
return image;
}