如果我在启用了ARC的项目中运行以下命令,则完成处理程序永远不会触发。但是没有ARC它按预期工作。我在这里缺少什么?
NSURL *url = [NSURL URLWithString:@"http://media.w3.org/2010/05/sintel/trailer.mp4"];
AVURLAsset *asset=[[AVURLAsset alloc] initWithURL:url options:nil];
AVAssetImageGenerator *generator = [[AVAssetImageGenerator alloc] initWithAsset:asset];
generator.appliesPreferredTrackTransform = YES;
CMTime thumbTime = CMTimeMakeWithSeconds(5,30);
AVAssetImageGeneratorCompletionHandler handler = ^(CMTime requestedTime,
CGImageRef im,
CMTime actualTime,
AVAssetImageGeneratorResult result,
NSError *error){
NSLog(@"completion handler");
};
generator.maximumSize = CGSizeMake(320, 180);
[generator generateCGImagesAsynchronouslyForTimes:[NSArray arrayWithObject:[NSValue valueWithCMTime:thumbTime]] completionHandler:handler];
答案 0 :(得分:5)
基于Michael指出生成器没有被保留,如果你只是在完成块内“使用”生成器var,一切都会有效。
所以我猜答案是......
NSURL *url = [NSURL URLWithString:@"http://media.w3.org/2010/05/sintel/trailer.mp4"];
AVURLAsset *asset=[[AVURLAsset alloc] initWithURL:url options:nil];
AVAssetImageGenerator *generator = [[AVAssetImageGenerator alloc] initWithAsset:asset];
generator.appliesPreferredTrackTransform = YES;
CMTime thumbTime = CMTimeMakeWithSeconds(5,30);
AVAssetImageGeneratorCompletionHandler handler = ^(CMTime requestedTime,
CGImageRef im,
CMTime actualTime,
AVAssetImageGeneratorResult result,
NSError *error){
NSLog(@"make sure generator is used in this block and it'll work %@", generator);
};
generator.maximumSize = CGSizeMake(320, 180);
[generator generateCGImagesAsynchronouslyForTimes:[NSArray arrayWithObject:[NSValue valueWithCMTime:thumbTime]] completionHandler:handler];
答案 1 :(得分:3)
看起来generateCGImagesAsynchronouslyForTimes不会保留生成器。使生成器成为您的实例的成员变量并分配给它。我一做到这一点就开始工作了。代码看起来像这样:
NSURL *url = [NSURL URLWithString:@"http://media.w3.org/2010/05/sintel/trailer.mp4"];
AVURLAsset *asset=[[AVURLAsset alloc] initWithURL:url options:nil];
[generator_ cancelAllCGImageGeneration];
generator_ = [[AVAssetImageGenerator alloc] initWithAsset:asset];
generator_.appliesPreferredTrackTransform = YES;
CMTime thumbTime = CMTimeMakeWithSeconds(5,30);
AVAssetImageGeneratorCompletionHandler handler = ^(CMTime requestedTime,
CGImageRef im,
CMTime actualTime,
AVAssetImageGeneratorResult result,
NSError *error){
NSLog(@"completion handler");
};
generator_.maximumSize = CGSizeMake(320, 180);
[generator_ generateCGImagesAsynchronouslyForTimes:[NSArray arrayWithObject:[NSValue valueWithCMTime:thumbTime]] completionHandler:handler];
答案 2 :(得分:0)
图像生成器肯定 不由框架保留。不确定Apple编程指南的更新频率,但您可以在AV Foundation Prog中阅读。指南,Generating a Sequence of Images非常清楚:
此外,您必须确保保留图像生成器 直到它完成创建图像。