AVAssetImageGeneratorCompletionHandler - how to set or return variables?

I use AVAssetImageGenerator to get images from a movie clip without playing it before. Now I have a question, how to set up variables in a handler loop? Is it possible? I get an error message and have no idea what that means. (google> no results).

"Variable not assigned (missing __block type specifier)"

So, I have to ask the professionals here. Here is the code. I want to save or return my generated images, so I can delete the message "setImage" inside this handler.

UIImage* thumbImg = [[UIImage alloc] init]; AVAssetImageGeneratorCompletionHandler handler = ^(CMTime requestedTime, CGImageRef im, CMTime actualTime, AVAssetImageGeneratorResult result, NSError *error) { if (result != AVAssetImageGeneratorSucceeded) { NSLog(@"couldn't generate thumbnail, error:%@", error); } [button setImage:[UIImage imageWithCGImage:im] forState:UIControlStateNormal]; thumbImg = [[UIImage imageWithCGImage:im] retain]; [generator release]; }; 

It would be great to know about this. Thank you for your time.

+6
source share
1 answer

First of all, it seems that you do not need to initialize your thumbImg when its declared - the UIImage object created in this line will be overwritten in the block and will leak. Just run it with a null value.

The actual problem in your code is that the variable you are going to change in the block must be declared using the __block specifier (as indicated in the error message). So your line 1s should be

 __block UIImage* thumbImg = nil; 
+21
source

Source: https://habr.com/ru/post/886923/


All Articles