ios 本地视频文件截图,有的类已经deprecated,提供三个函数以供使用
+ (UIImage*)getVideoThumbnailWithUrl:(NSURL*)videoUrl second:(CGFloat)second size:(CGSize)size
{
if (!videoUrl)
{
NSLog(@"WARNING:videoUrl为空");
return nil;
}
AVURLAsset *urlSet = [AVURLAsset assetWithURL:videoUrl];
AVAssetImageGenerator *imageGenerator = [AVAssetImageGenerator assetImageGeneratorWithAsset:urlSet];
imageGenerator.appliesPreferredTrackTransform = YES;
imageGenerator.apertureMode = AVAssetImageGeneratorApertureModeEncodedPixels;
/*
如果不需要获取缩略图,就设置为NO,如果需要获取缩略图,则maximumSize为获取的最大尺寸。
以BBC为例,getThumbnail = NO时,打印宽高数据为:1920*1072。
getThumbnail = YES时,maximumSize为100*100。打印宽高数据为:100*55.
注:不乘[UIScreen mainScreen].scale,会发现缩略图在100*100很虚。
*/
BOOL getThumbnail = YES;
if (getThumbnail)
{
CGFloat width = [UIScreen mainScreen].scale * size.width;
CGFloat height = [UIScreen mainScreen].scale * size.height;
imageGenerator.maximumSize = CGSizeMake(height, width);
}
NSError *error = nil;
CMTime time = CMTimeMake(second,k_FPS);
CMTime actucalTime;
CGImageRef cgImage = [imageGenerator copyCGImageAtTime:time actualTime:&actucalTime error:&error];
if (error) {
NSLog(@"ERROR:获取视频图片失败,%@",error.domain);
}
CMTimeShow(actucalTime);
UIImage *image = [UIImage imageWithCGImage:cgImage];
NSLog(@"imageWidth=%f,imageHeight=%f",image.size.width,image.size.height);
CGImageRelease(cgImage);
return image;
}
+(UIImage*)getVideoImageWithUrl:(NSURL*)videoUrl second:(CGFloat)second{
MPMoviePlayerController *moviePlayer = [[MPMoviePlayerController alloc]initWithContentURL:videoUrl];
moviePlayer.shouldAutoplay = NO;
UIImage *thumbnail = [moviePlayer thumbnailImageAtTime:second timeOption:MPMovieTimeOptionNearestKeyFrame];
return thumbnail;
}
+(UIImage*)getFristFrameImageWithUrl:(NSURL*)videoUrl{
MPMoviePlayerController *mp = [[MPMoviePlayerController alloc]
initWithContentURL:videoUrl];
UIImage *img = [mp thumbnailImageAtTime:0.0
timeOption:MPMovieTimeOptionNearestKeyFrame];
[mp stop];
return img;
}
第一个函数的size填你得视频的尺寸为最佳
原文地址:https://www.cnblogs.com/RoysPhoneBlog/p/9869240.html
时间: 2024-11-05 19:11:14