首先我们要导入三个官方的库
#import <AVFoundation/AVAsset.h>
#import <AVFoundation/AVAssetImageGenerator.h>
#import <AVFoundation/AVTime.h>
导入这三个库就是获取的方法了,一种是本地的视频,另一种是网络视频
// 获取视频第一帧
- (UIImage*) getVideoPreViewImage:(NSString *)path{
//本地视频
AVURLAsset *asset = [[AVURLAsset alloc] initWithURL:[NSURL fileURLWithPath:path] options:nil];
//网络视频
AVURLAsset *asset2 = [[AVURLAsset alloc] initWithURL:[NSURL URLWithString:path] options:nil];
AVAssetImageGenerator *assetGen = [[AVAssetImageGenerator alloc] initWithAsset:asset];
assetGen.appliesPreferredTrackTransform = YES;
CMTime time = CMTimeMakeWithSeconds(0.0, 600);
NSError *error = nil;
CMTime actualTime;
CGImageRef image = [assetGen copyCGImageAtTime:time actualTime:&actualTime error:&error];
UIImage *videoImage = [[UIImage alloc] initWithCGImage:image];
CGImageRelease(image);
if (videoImage == nil) {
videoImage = [UIImage imageNamed:@"default_video.png"];
}
return videoImage;
}