I am trying to get an array of images from video. It is working well but I have a doubt that In which thread the completion handler get called.
I called this method(
generateCGImagesAsynchronouslyForTimes:
) in new operation and I updated the UI in the completion handler. The UI get updated.But typically the UI updation doesn't happen in secondary thread?. My doubt is the completion handler called in current calling thread or main thread?
My Code is:
__block unsigned int i = 0;
AVAssetImageGeneratorCompletionHandler handler = ^(CMTime requestedTime, CGImageRef im, CMTime actualTime, AVAssetImageGeneratorResult result, NSError *error){
i++;
if(result == AVAssetImageGeneratorSucceeded){
//Create a block to save the image in disk
NSBlockOperation *operation = [NSBlockOperation blockOperationWithBlock:^{
NSFileManager *fileMgr = [NSFileManager defaultManager];
NSString *documentsDirectory = [NSHomeDirectory()
stringByAppendingPathComponent:@"Documents"];
NSError *error = nil;
//Create file path for storing the image
NSString *videoOutputPath = [documentsDirectory stringByAppendingPathComponent:[NSString stringWithFormat:@"VideoFrames%i.png", i]];
//Delete if already any image exist
if ([fileMgr fileExistsAtPath:videoOutputPath]){
if ([fileMgr removeItemAtPath:videoOutputPath error:&error] != YES)
NSLog(@"Unable to delete file: %@", [error localizedDescription]);
}
//Convert the CGImageRef to UIImage
UIImage *image = [UIImage imageWithCGImage:im];//**This line gives error: EXE_BAD_ACCESS**
//Save the image
if(![UIImagePNGRepresentation(image) writeToFile:videoOutputPath options:NSDataWritingFileProtectionNone error:&error])
NSLog(@"Failed to save image at path %@", videoOutputPath);
}];
//Add the operation to the queue
[self.imageWritingQueue addOperation:operation];
}
}
};