欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  移动技术

iOS将视频录像切成一张张缩略图

程序员文章站 2024-02-15 09:20:46
本文实例为大家分享了ios视频录像切成缩略图的具体代码,供大家参考,具体内容如下 记得导入系统库 #import < mediaplayer/mediapla...

本文实例为大家分享了ios视频录像切成缩略图的具体代码,供大家参考,具体内容如下

记得导入系统库

#import < mediaplayer/mediaplayer.h >

代码:

/**
 * 获取网络视频的全部缩略图方法
 *
 * @param videourl 视频的链接地址
 *
 * @return 视频截图
 */
+ (uiimage *)ihefe_previewimagewithvideourl:(nsurl *)videourl
{
 avasset *asset = [avasset assetwithurl:videourl];

 avassetimagegenerator *generator = [avassetimagegenerator assetimagegeneratorwithasset:asset];
 generator.appliespreferredtracktransform = yes;

 cgimageref img = [generator copycgimageattime:cmtimemake(1, asset.duration.timescale) actualtime:null error:nil];
 uiimage *image = [uiimage imagewithcgimage:img];

 cgimagerelease(img);
 return image;
}

/**
 * 获取本地视频的全部缩略图方法
 *
 * @param fileurl 视频的链接地址
 *
 * @return 视频截图
 */
+ (uiimage *)ihefe_getscreenshotimagefromvideourl:(nsstring *)fileurl
{

 uiimage *shotimage;
 //视频路径url
 nsurl *fileurl = [nsurl urlwithstring:fileurl];

 avurlasset *asset = [[avurlasset alloc] initwithurl:fileurl 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];

 shotimage = [[uiimage alloc] initwithcgimage:image];

 cgimagerelease(image);

 return shotimage;
}

/**
 * 获取视频的某一帧缩略图方法
 *
 * @param videourl 视频的链接地址 帧时间
 * @param time  帧时间
 *
 * @return 视频截图
 */
+ (uiimage*)ihefe_thumbnailimageforvideo:(nsurl *)videourl attime:(nstimeinterval)time
{
 avurlasset *asset = [[avurlasset alloc] initwithurl:videourl options:nil];
 nsparameterassert(asset);
 avassetimagegenerator *assetimagegenerator = [[avassetimagegenerator alloc] initwithasset:asset];
 assetimagegenerator.appliespreferredtracktransform = yes;
 assetimagegenerator.aperturemode = avassetimagegeneratoraperturemodeencodedpixels;

 cgimageref thumbnailimageref = null;
 cftimeinterval thumbnailimagetime = time;
 nserror *thumbnailimagegenerationerror = nil;
 thumbnailimageref = [assetimagegenerator copycgimageattime:cmtimemake(thumbnailimagetime, 60) actualtime:null error:&thumbnailimagegenerationerror];

 if (!thumbnailimageref) nslog(@"thumbnailimagegenerationerror %@", thumbnailimagegenerationerror);

 uiimage *thumbnailimage = thumbnailimageref ? [[uiimage alloc] initwithcgimage:thumbnailimageref] : nil;

 return thumbnailimage;
}

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。