#import "ViewController.h"#import@interface ViewController ()@property (weak, nonatomic) IBOutlet UIImageView *imageView;@end@implementation ViewController- (void)viewDidLoad { [super viewDidLoad]; }- (void)touchesBegan:(NSSet*)touches withEvent:(UIEvent *)event
{
// 获取视频url
NSURL *url = [[NSBundle mainBundle] URLForResource:@"Alizee_La_Isla_Bonita.mp4" withExtension:nil];
// 设置截图的资源
AVAsset *asset = [AVAsset assetWithURL:url];
// 创建视频截图对象
AVAssetImageGenerator *generator = [[AVAssetImageGenerator alloc] initWithAsset:asset];
// 开始截图
// CMTimeMake 参数1 / 参数2 = 时间
CMTime time = CMTimeMake(30, 2);
NSValue *value = [NSValue valueWithCMTime:time];
// 参数1 :截图的时间
[generator generateCGImagesAsynchronouslyForTimes:@[value] completionHandler:^(CMTime requestedTime, CGImageRef _Nullable image, CMTime actualTime, AVAssetImageGeneratorResult result, NSError * _Nullable error) {
if (error) {
NSLog(@"%@",error);
}
UIImage *uiimage = [UIImage imageWithCGImage:image];
[self performSelectorOnMainThread:@selector(updateUI:) withObject:uiimage waitUntilDone:NO];
}];
}
- (void)updateUI:(UIImage *)image
{
self.imageView.image = image;
}
@end
网友评论