AVAssetImageGenerator 是用来提供视频的缩略图或预览视频的帧的类.
可以用AVAsset来初始化: initWithAsset: 或
assetImageGeneratorWithAsset:
(生成图片的真正时间可能在requestedTimeToleranceBefore
和
requestedTimeToleranceAfter之间,也可能和请求时间无关.)
可以用异步方法获取一系列视频的帧:
generateCGImagesAsynchronouslyForTimes:completionHandler:
copyCGImageAtTime:actualTime:error:
另附代码如下://创建URL NSURL *url=[NSURL URLWithString:[_videoURLStr stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]]; AVURLAsset *urlAsset=[AVURLAsset assetWithURL:url]; AVAssetImageGenerator *imageGenerator=[AVAssetImageGenerator assetImageGeneratorWithAsset:urlAsset]; NSError *error=nil; CMTime time=CMTimeMakeWithSeconds(timeBySecond, 10); CMTime actualTime; CGImageRef cgImage= [imageGenerator copyCGImageAtTime:time actualTime:&actualTime error:&error]; if(error){ return; } CMTimeShow(actualTime); UIImage *image=[UIImage imageWithCGImage:cgImage];//转化为UIImage //保存到相册 UIImageWriteToSavedPhotosAlbum(image,nil, nil, nil); CGImageRelease(cgImage);