iosobjective-canimated-gifmbprogresshud

MBProgressHud with gif image


Can I use gif image instead of default loading indicator? I am using this code so far but not getting any result. Can anyone suggest what is wrong in this code?

#import "UIImage+GIF.h"
-(void) showLoadingHUD:(NSString *)title
{
    [self hideLoadingHUD];
    if(!HUD){
        HUD = [MBProgressHUD showHUDAddedTo:self.window animated:YES];
    }
    [HUD setColor:[UIColor clearColor]];

    UIImageView *imageViewAnimatedGif = [[UIImageView alloc]init];
    imageViewAnimatedGif.image= [UIImage sd_animatedGIFNamed:@"martini_glass"];

    HUD.customView = [[UIImageView alloc] initWithImage:imageViewAnimatedGif.image];
    CABasicAnimation *rotation;
    rotation = [CABasicAnimation animationWithKeyPath:@"transform.rotation"];
    rotation.fromValue = [NSNumber numberWithFloat:0];
    rotation.toValue = [NSNumber numberWithFloat:(2 * M_PI)];
    rotation.duration = 0.7f; // Speed
    rotation.repeatCount = HUGE_VALF; // Repeat forever. Can be a finite number.
    [HUD.customView.layer addAnimation:rotation forKey:@"Spin"];
    HUD.mode = MBProgressHUDModeCustomView;
    [HUD show:YES];
}

Solution

  • use latest libraries of MBProgressHUD and SDWebImage for "UIImage+GIF.h" and it is working fine

    -(void) showLoadingHUD:(NSString *)title {
    
        [HUD hideAnimated:true];
        HUD = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    
        HUD.label.text = title;
        HUD.bezelView.color = [UIColor clearColor];
        UIImageView *imageViewAnimatedGif = [[UIImageView alloc]init];
    
        //The key here is to save the GIF file or URL download directly into a NSData instead of making it a UIImage. Bypassing UIImage will let the GIF file keep the animation.
        NSString *filePath = [[NSBundle mainBundle] pathForResource: @"loader" ofType: @"gif"];
        NSData *gifData = [NSData dataWithContentsOfFile: filePath];
        imageViewAnimatedGif.image = [UIImage sd_animatedGIFWithData:gifData];
    
        HUD.customView = [[UIImageView alloc] initWithImage:imageViewAnimatedGif.image];
        CABasicAnimation *rotation;
        rotation = [CABasicAnimation animationWithKeyPath:@"transform.rotation"];
        rotation.fromValue = [NSNumber numberWithFloat:0];
        rotation.toValue = [NSNumber numberWithFloat:(2 * M_PI)];
        rotation.duration = 0.7f; // Speed
        rotation.repeatCount = HUGE_VALF; // Repeat forever. Can be a finite number.
        rotation.removedOnCompletion = false;
        [HUD.customView.layer addAnimation:rotation forKey:@"Spin"];
        HUD.mode = MBProgressHUDModeCustomView;
        HUD.contentColor = [UIColor redColor];
        [HUD showAnimated:YES];
    }
    

    sample loader .gif image: loader