1

我有这个问题,我从过去 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");
    }
}];
4

2 回答 2

0

这是我在我的一个应用程序中使用的代码,该应用程序目前在应用程序商店中可用并且工作得很好。此代码获取视频的第一帧并从中生成图像。

AVURLAsset *asset1 = [[AVURLAsset alloc] initWithURL:[NSURL fileURLWithPath:toPath] options:nil];
            AVAssetImageGenerator *generate1 = [[AVAssetImageGenerator alloc] initWithAsset:asset1];
            generate1.appliesPreferredTrackTransform = YES;
            NSError *err = NULL;
            CMTime time = CMTimeMake(1, 2);
            CGImageRef oneRef = [generate1 copyCGImageAtTime:time actualTime:NULL error:&err];
            UIImage *image = [[UIImage alloc] initWithCGImage:oneRef];

希望这段代码对你有用。让我知道你的评论 :) 快乐的编码伙伴 :)

于 2015-12-29T06:46:13.237 回答
0
    imageGenerator.requestedTimeToleranceBefore = kCMTimeZero;
    imageGenerator.requestedTimeToleranceAfter = kCMTimeZero;

I added these two lines and now i get the perfect result that I needed.I still am not 100% sure what these properties do but I got what I needed.Now if anyone explains about these properties,it will save me some time as I will be exploring these properties sooner or later.Just copy the code and you will see everything works perfectly fine now. :-)

于 2016-01-03T14:56:49.787 回答