根据视频资源的路径获取视频的缩略图
+(UIImage *)getThumbnailImage:(NSString *)videoPath {
if (videoPath) {
AVURLAsset *asset = [[AVURLAsset alloc] initWithURL:[NSURL fileURLWithPath: videoPath] options:nil];
AVAssetImageGenerator *gen = [[AVAssetImageGenerator alloc] initWithAsset:asset];
// 设定缩略图的方向
// 如果不设定,可能会在视频旋转90/180/270°时,获取到的缩略图是被旋转过的,而不是正向的
gen.appliesPreferredTrackTransform = YES;
// 设置图片的最大size(分辨率)
gen.maximumSize = CGSizeMake(300, 169);
CMTime time = CMTimeMakeWithSeconds(5.0, 600); //取第5秒,一秒钟600帧
NSError *error = nil;
CMTime actualTime;
CGImageRef image = [gen copyCGImageAtTime:time actualTime:&actualTime error:&error];
if (error) {
UIImage *placeHoldImg = [UIImage imageNamed:@"posters_default_horizontal"];
return placeHoldImg;
}
UIImage *thumb = [[UIImage alloc] initWithCGImage:image];
CGImageRelease(image);
return thumb;
} else {
UIImage *placeHoldImg = [UIImage imageNamed:@"posters_default_horizontal"];
return placeHoldImg;
}
}
// 获取视频第一帧
-
(UIImage*) getVideoPreViewImage:(NSURL *)path
{
AVURLAsset *asset = [[AVURLAsset alloc] initWithURL:path options:nil];
AVAssetImageGenerator *assetGen = [[AVAssetImageGenerator alloc] initWithAsset:asset];assetGen.appliesPreferredTrackTransform = YES;
CMTime time = CMTimeMakeWithSeconds(0.0, 600);
NSError *error = nil;
CMTime actualTime;
CGImageRef image = [assetGen copyCGImageAtTime:time actualTime:&actualTime error:&error];
UIImage *videoImage = [[UIImage alloc] initWithCGImage:image];
CGImageRelease(image);
return videoImage;
}
网友评论