Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

generateCGImagesAsynchronouslyForTimes in ARC

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];
like image 499
dizy Avatar asked Feb 21 '23 19:02

dizy


1 Answers

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...

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];
like image 55
dizy Avatar answered Mar 04 '23 06:03

dizy