gpt4 book ai didi

ios - iOS7 中的 AVAssetImageGenerator 随机失败

转载 作者:可可西里 更新时间:2023-11-01 04:52:41 24 4
gpt4 key购买 nike

我有一个 AVComposition 和一个 AVVideoComposition,我会为视频的每半秒左右生成一个缩略图。我使用动画工具进行一些叠加。我对操作队列执行调用,它在某些类型的编辑后触发。操作队列是串行的而不是异步的,所以进程永远不会并发运行,我已经验证了这一点。下面的代码在串行队列中的另一个 NSOperation 的主线程上执行。

现在我的问题是,拇指随机失败,没有模式。我可以运行一次并让它全部成功。我可以使用完全相同的参数再次运行它,并且有一些失败。我可以再次运行它并让不同的拇指失败。

这似乎与其他线程中正在进行的其他事件的数量有关,例如从 ALAssetLibrary 或 MediaFramework 中查询大量项目。

我还要提到这在 iOS6 中没有这个问题。

在生成缩略图之前,我确实创建了 AVComposition 和 AVVideoComposition 的副本。

有人给我指点吗?

我很感激。

//this must be called on the main thread to correctly generate overlays
[[NSOperationQueue mainQueue] addOperationWithBlock:^{

AVAsset *asset = avComposition;

AVAssetImageGenerator *generator = [[AVAssetImageGenerator alloc] initWithAsset:asset];
generator.appliesPreferredTrackTransform = TRUE;

AVMutableVideoComposition *mutableVideoComposition = (AVMutableVideoComposition *) avVideoComposition.mutableCopy;
CALayer *baseSyncLayer = [CALayer layer];
baseSyncLayer.frame = CGRectMake(0, 0, resolution.width, resolution.height);
CALayer *videoLayer = [CALayer layer];
videoLayer.frame = CGRectMake(0, 0, resolution.width, resolution.height);
[baseSyncLayer addSublayer:videoLayer];
__unused NSArray *imageLayers = [MovieCompositionService applyImageAnimations:avComposition andLayer:baseSyncLayer andProject:project fileRender:YES addTextOverlays:addTextOverlays andResolution:resolution];
mutableVideoComposition.animationTool = [AVVideoCompositionCoreAnimationTool videoCompositionCoreAnimationToolWithPostProcessingAsVideoLayer:videoLayer inLayer:baseSyncLayer];
generator.videoComposition = mutableVideoComposition;

NSMutableArray *images = [[NSMutableArray alloc] init];

__block BOOL hasErrors = NO;

AVAssetImageGeneratorCompletionHandler handler = ^(CMTime requestedTime, CGImageRef im, CMTime actualTime, AVAssetImageGeneratorResult result, NSError *error) {
if (!hasErrors) {

DDLogVerbose(@"ACTUAL GENERATION TIME %f", CMTimeGetSeconds(actualTime));

if (result != AVAssetImageGeneratorSucceeded) {
DDLogError(@"Failed to generate thumb: %@", error.localizedDescription);
hasErrors = YES;
[generator cancelAllCGImageGeneration];
finishBlock(images, YES);
} else {
[images addObject:[UIImage imageWithCGImage:im]];

if (CMTIME_COMPARE_INLINE(requestedTime, ==, ((NSValue *) timeIntervals.lastObject).CMTimeValue)) {
DDLogVerbose(@"Movie composition thumb generation complete");
finishBlock(images, NO);
}
}
}
};

generator.maximumSize = thumbSize;
[generator generateCGImagesAsynchronouslyForTimes:timeIntervals completionHandler:handler];
}];

最佳答案

我使用以下内容生成缩略图。

- (void)getThumbnailImageForTime:(float)instance_time withCompletion:(void(^)(UIImage *))completion
{
dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0ul);
dispatch_async(queue, ^{
__block UIImage *thumb ;
AVPlayerItem *playerItem = [AVPlayerItem playerItemWithURL:self.videoUrl];
AVAssetImageGenerator *_generator;
_generator = [AVAssetImageGenerator assetImageGeneratorWithAsset:playerItem.asset];

AVAssetImageGeneratorCompletionHandler handler = ^(CMTime requestedTime, CGImageRef image, CMTime actualTime, AVAssetImageGeneratorResult result, NSError *error) {
if (result == AVAssetImageGeneratorSucceeded) {
thumb = [UIImage imageWithCGImage:image];
NSLog(@"Succesfully generater the thumbnail!!!");
} else {
NSLog(@"Failed to generater the thumbnail!!!");
NSLog(@"Error : %@",error.localizedDescription);
[self performSelector:@selector(retryingThumbnialGeneration) withObject:nil afterDelay:1.0f];
}
dispatch_async(dispatch_get_main_queue(), ^{
completion(thumb);
});
};

[_generator generateCGImagesAsynchronouslyForTimes:[NSArray arrayWithObject:[NSValue valueWithCMTime:CMTimeMakeWithSeconds(2,10)]] completionHandler:handler];

});

}

关于ios - iOS7 中的 AVAssetImageGenerator 随机失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18919254/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com