Show activity indicator in SDWebImage

前端 未结 18 1383
独厮守ぢ
独厮守ぢ 2021-01-30 09:20

I\'m using SDWebView image and i want to show an Activity Indicator as placeholder, while fetching the image from remote.

I tried Malek\'s answer here How to show an act

18条回答
  •  一生所求
    2021-01-30 09:40

    Based on Can Ürek's answer you might wanna create a category to make it easier to use across multiple applications and without modify SDWebImage framework.

    Header file:

    #import 
    
    #import 
    
    @interface UIImageView (WebCacheWithActivityIndicator)
    
    - (void)setImageShowingActivityIndicatorWithURL:(NSURL *)url completed:(SDWebImageCompletedBlock)completedBlock;
    
    @end
    

    Implementation file:

    #import "UIImageView+WebCacheWithActivityIndicator.h"
    
    @implementation UIImageView (WebCacheWithActivityIndicator)
    
    - (void)setImageShowingActivityIndicatorWithURL:(NSURL *)url completed:(SDWebImageCompletedBlock)completedBlock
    {
        UIActivityIndicatorView* activityIndication = [[UIActivityIndicatorView alloc]initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray];
    
        [activityIndication setFrame:CGRectMake((self.frame.size.width - activityIndication.frame.size.width) / 2 , (self.frame.size.height - activityIndication.frame.size.height) / 2 , activityIndication.frame.size.width , activityIndication.frame.size.width)];
        [self addSubview:activityIndication];
    
        [activityIndication startAnimating];
    
        [self setImageWithURL:url completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType) {
    
            if(completedBlock)
            {
                completedBlock(image,error,cacheType);
            }
    
            [activityIndication stopAnimating];
            [activityIndication removeFromSuperview];
        }];
    }
    @end
    

    Hope it helps you out guys

    Cheers

提交回复
热议问题