I am using the code below to grab a farme from a video to use as a thumb nail. This is working as desired, however my MBProgressHUD is only showing up after the image grab has been completed, flashing on and off the screen in a second. I have used MBPRogressHUD several times in the same way, displaying it at the start of the code and hiding after everything else is done, which has always worked but this time it is like the code is running out of order? Any help will be much appreciated.
This is the method called on my button press.
- (IBAction)grabImage:(id)sender {
MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
hud.mode = MBProgressHUDModeIndeterminate;
hud.labelText = @"Grabing Thumbnail";
[hud show:YES];
self.imageholder.image = [self grabImageMethod];
[hud hide:YES];
}
And this is the grab method called with in.
-(UIImage*)grabImageMethod{
NSURL *vidURL = [NSURL URLWithString:@"http://myserver.com/myfile.mp4"];
AVURLAsset *asset = [[AVURLAsset alloc] initWithURL:vidURL options:nil];
AVAssetImageGenerator *generate = [[AVAssetImageGenerator alloc] initWithAsset:asset];
NSError *err = NULL;
CMTime time = CMTimeMake(1, 60);
CGImageRef imgRef = [generate copyCGImageAtTime:time actualTime:NULL error:&err];
NSLog(@"err==%@, imageRef==%@", err, imgRef);
return [[UIImage alloc] initWithCGImage:imgRef];
}
After the comment from nielsbot i went and replaced
[hud show:YES];
self.imageholder.image = [self grabImageMethod];
[hud hide:YES];
with
[hud showAnimated:YES whileExecutingBlock:^{
self.imageholder.image = [self grabImageMethod];
}];
It now works perfectly.