亲宝软件园·资讯

展开

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

Hanrovey 人气:1
想了解iOS将视频录像切成一张张缩略图的相关内容吗,Hanrovey在本文为您仔细讲解iOS视频录像切成缩略图的相关知识和一些Code实例,欢迎阅读和指正,我们先划重点: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;
}

加载全部内容

相关教程
猜你喜欢
用户评论