截取指定时间的视频缩略图

OC 截取指定时间的视频缩略图

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
/**
* 截取指定时间的视频缩略图
*
* @param timeBySecond 时间点
*/
-(void)thumbnailImageRequest:(CGFloat )timeBySecond{
//创建URL
NSURL *url=[self getFileUrl];
//根据url创建AVURLAsset
AVURLAsset *urlAsset=[AVURLAsset assetWithURL:url];
//根据AVURLAsset创建AVAssetImageGenerator
AVAssetImageGenerator *imageGenerator=[AVAssetImageGenerator assetImageGeneratorWithAsset:urlAsset];
/*截图
* requestTime:缩略图创建时间
* actualTime:缩略图实际生成的时间
*/
NSError *error=nil;
CMTime time=CMTimeMakeWithSeconds(timeBySecond, 10);//CMTime是表示电影时间信息的结构体,第一个参数是视频第几秒,第二个参数时每秒帧数.(如果要活的某一秒的第几帧可以使用CMTimeMake方法)
CMTime actualTime;
CGImageRef cgImage= [imageGenerator copyCGImageAtTime:time actualTime:&actualTime error:&error];
if(error){
NSLog(@"截取视频缩略图时发生错误,错误信息:%@",error.localizedDescription);
return;
}
CMTimeShow(actualTime);
UIImage *image=[UIImage imageWithCGImage:cgImage];//转化为UIImage
//保存到相册
UIImageWriteToSavedPhotosAlbum(image,nil, nil, nil);
CGImageRelease(cgImage);
}

Swift 保存图片到相册

1
2
3
4
5
6
7
8
9
10
/// 保存图片到相册
func saveAction(sender:AnyObject?){
UIImageWriteToSavedPhotosAlbum(imageView.image, self,"image:didFinishSavingWithError:contextInfo:", nil)
}
func image(image: UIImage, didFinishSavingWithError: NSError?, contextInfo: AnyObject) {
if didFinishSavingWithError != nil {
print ("错误")
return
}
}

本文标题:截取指定时间的视频缩略图

文章作者:史彦超

发布时间:2016年10月11日 - 22:10

最后更新:2021年07月20日 - 16:07

原始链接:https://doingself.github.io/2016/10/11/2016-10-11-%E6%88%AA%E5%8F%96%E6%8C%87%E5%AE%9A%E6%97%B6%E9%97%B4%E7%9A%84%E8%A7%86%E9%A2%91%E7%BC%A9%E7%95%A5%E5%9B%BE/

许可协议: 署名-非商业性使用-禁止演绎 4.0 国际 转载请保留原文链接及作者。

Donate comment here