#import "ViewController.h" #import <AVFoundation/AVFoundation.h> @interface ViewController () @property (weak, nonatomic) IBOutlet UIImageView *imageView; @property(nonatomic ,strong)AVAssetImageGenerator * generatorl; @end @implementation ViewController - (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view, typically from a nib. } -(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{ //获取资源路径 NSURL * url = [[NSBundle mainBundle]URLForResource:@"Alizee_La_Isla_Bonita副本.mp4" withExtension:nil]; //包装 AVAsset * set = [AVAsset assetWithURL:url]; //创建视频截取器 AVAssetImageGenerator * generator = [AVAssetImageGenerator assetImageGeneratorWithAsset:set]; self.generatorl = generator; CMTime time = CMTimeMake(10, 1); // NSValue * value = [NSValue valueWithCMTime:time]; //截取视频 ( 这个方法截取的不准确) [generator generateCGImagesAsynchronouslyForTimes:@[value] completionHandler:^(CMTime requestedTime, CGImageRef _Nullable image, CMTime actualTime, AVAssetImageGeneratorResult result, NSError * _Nullable error) { UIImage * images = [UIImage imageWithCGImage:image]; dispatch_sync(dispatch_get_main_queue(), ^{ self.imageView.image = images; }); }]; }
时间: 2024-10-06 16:42:03