在 ARC 中异步生成 CGImagesForTimes
如果我在启用了 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];
If I run the following in a project where ARC is enabled the completion handler never fires. But without ARC it works as expected. What am I missing here?
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];
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
根据 Michael 指出,生成器不会被保留,如果您只是在完成块中“使用”生成器 var,一切都会正常。
所以我想答案是...
Based on Michael pointing out that generator doesn't get retained, if you simply 'use' the generator var inside the completion block, everything will work.
So I guess the answer is...
看起来generateCGImagesAsynchronouslyForTimes不保留生成器。使生成器成为实例的成员变量并分配给它。我一这样做,它就开始工作了。代码看起来像这样:
Looks like generateCGImagesAsynchronouslyForTimes does not retain the generator. Make generator a member variable of your instance and assign to it. As soon as I did that, it started working. The code will look something like this:
图像生成器肯定不会被框架保留。不确定 Apple 编程指南的更新频率,但您可以在 AV Foundation Prog 中阅读该指南。指南,位于 生成图像序列非常清楚:
The image generator definitely does not get retained by the framework. Not sure how often Apple Programming Guide gets updated, but you can read that in AV Foundation Prog. Guide, in Generating a Sequence of Images quite clearly: