How to show an activity indicator in SDWebImage

后端 未结 6 970
一向
一向 2020-12-25 12:50

Currently i am integrating SDWebImage in my project by following below things

1)#import \"UIButton+WebCache.h\"

2)[button setImageWithURL:url placeholderImag

6条回答
  •  春和景丽
    2020-12-25 13:48

    the best way is to add the activityIndicator in all "setImage" functions in UIImageView+WebCache.m, after that you remove it in "webImageManager:r didFinishWithImage:" , i test it in device and it work smoothly, her's an example :

      - (void)setImageWithURL:(NSURL *)url placeholderImage:(UIImage *)placeholder
     {
    
    
    UIActivityIndicatorView *activity = [[UIActivityIndicatorView alloc]initWithActivityIndicatorStyle:(UIActivityIndicatorViewStyleWhiteLarge)];
    [activity startAnimating];
    [activity setCenter:self.center];
    [self addSubview:activity];
    [activity release];
    
    
    
    SDWebImageManager *manager = [SDWebImageManager sharedManager];
    
    UIImage *cachedImage = [manager imageWithURL:url];
    
    if (cachedImage)
    {
        // the image is cached -> remove activityIndicator from view
        for (UIView *v in [self subviews])
        {
            if ([v isKindOfClass:[UIActivityIndicatorView class]])
            {
                [activity removeFromSuperview];
            }
        }
    }
    
    [self setImageWithURL:url placeholderImage:placeholder options:0];
    }
    

    and you remove it with a fade animation ;) :

     - (void)webImageManager:(SDWebImageManager *)imageManager didFinishWithImage:(UIImage *)image
    { 
    
    for (UIView *v in [self subviews])
    {
        if ([v isKindOfClass:[UIActivityIndicatorView class]])
        {
           // NSLog(@"-didFinishWithImage-");
            [((UIActivityIndicatorView*)v) stopAnimating];
            [v removeFromSuperview];
        }
    }
    
    
    [UIView beginAnimations:@"fadeAnimation" context:NULL]; 
    [UIView setAnimationDuration:0.9];
    self.alpha = 0;
    self.image = image;
    self.alpha=1;
    //[UIView setAnimationTransition:UIViewAnimationTransitionFlipFromLeft forView:self cache:YES]; 
    [UIView commitAnimations];
    
     }
    

提交回复
热议问题