我有这个问题,我从过去 2 天现在面临。我需要从视频中生成图像。AVAssetImageGenerator
我正在使用并且我正在使用该方法
'generateCGImagesAsynchronouslyForTimes:times completionHandler:^(CMTime requestedTime, CGImageRef image, CMTime actualTime, AVAssetImageGeneratorResult result, NSError * _Nullable error)'
现在我在一个数组中获取图像,这些图像根据打印描述而有所不同。但是当我将这些图像设置在一个上时UIImage
,我丢失了一些图像并且我得到了一些重复的图像。这是我的代码'一直在使用我所做的:
- (IBAction)editButtonClicked:(id)sender {
AVAssetImageGenerator *imageGenerator = [AVAssetImageGenerator assetImageGeneratorWithAsset:self.asset];
imageGenerator.maximumSize = CGSizeMake(200.0f,0.0f);
CMTime duration = self.asset.duration;
NSMutableArray *times = [NSMutableArray array];
CMTimeValue increment = duration.value/20;
NSLog(@"%lld",increment);
CMTimeValue currentValue = 0;
while (currentValue <= duration.value) {
NSLog(@"%lld",increment);
NSLog(@"currentValue==%lld",currentValue);
CMTime time = CMTimeMake(currentValue, duration.timescale);
CMTimeShow(time);
[times addObject:[NSValue valueWithCMTime:time]];
currentValue+=increment;
NSLog(@"%lld",increment);
NSLog(@"currentValue===%lld",currentValue);
}
__block NSUInteger imageCount = times.count;
__block NSMutableArray *images = [NSMutableArray array];
__block AVAssetImageGeneratorCompletionHandler handler;
[imageGenerator generateCGImagesAsynchronouslyForTimes:times completionHandler:^(CMTime requestedTime, CGImageRef image, CMTime actualTime, AVAssetImageGeneratorResult result, NSError * _Nullable error) {
static NSInteger counter = 0;
if (result == AVAssetImageGeneratorSucceeded) {
UIImage *image1 = [UIImage imageWithCGImage:image];
[images addObject:image1];
NSLog(@"%@",images);
counter++;
NSLog(@"%ld",(long)counter);
if(counter == imageCount){
NSLog(@"inside if");
dispatch_async(dispatch_get_main_queue(), ^{ // 2
MyTableViewController *myTableVC = [self.storyboard instantiateViewControllerWithIdentifier:@"MyTableViewController"];
myTableVC.images = [images mutableCopy];
[self.navigationController pushViewController:myTableVC animated:YES];
});
}
}
else{
NSLog(@"Failed to create thumbNail image");
}
}];