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

时间:2022-06-01 21:07:08

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

记得导入系统库

#import < MediaPlayer/MediaPlayer.h >

代码:

?
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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
/**
 * 获取网络视频的全部缩略图方法
 *
 * @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;
}

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

原文链接:http://blog.csdn.net/hanrovey/article/details/53316766