Show activity indicator in SDWebImage

前端 未结 18 1364
独厮守ぢ
独厮守ぢ 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:35

    For Swift 5.0 and SDWebImage 5.0 :

    Replace

    imageView.sd_setShowActivityIndicatorView(true)
    imageView.sd_setIndicatorStyle(.gray)
    

    By

    imageView.sd_imageIndicator = SDWebImageActivityIndicator.gray
    
    0 讨论(0)
  • 2021-01-30 09:35

    SdWebImage 5.0

    YOUR_IMAGEVIEW.sd_imageIndicator = SDWebImageActivityIndicator.gray
    
    0 讨论(0)
  • 2021-01-30 09:36

    With updated library (Resolved Crash on iOS 8), we have below method -

    - (void)sd_setImageWithURL:(NSURL *)url 
    {
        [self sd_setImageWithURL:url placeholderImage:nil options:0 progress:nil completed:nil];
    }
    

    As we can see the option to add progress block and completed block, we can simply add Activity indicator in progress block and remove in completion block.

    Customized method to add Activity Indicator here -

    - (void)sd_setImageWithURL:(NSURL *)url {
    
        [self sd_setImageWithURL:url placeholderImage:nil options:0 progress:^(NSInteger receivedSize, NSInteger expectedSize) {
            UIActivityIndicatorView *activity = nil;
            activity = (UIActivityIndicatorView *)[self viewWithTag:100000];
            if (!activity) {
                activity = [[UIActivityIndicatorView alloc]initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray];
                [activity setTag:100000];
                [activity setHidesWhenStopped:YES];
                [activity setCenter:CGPointMake(self.frame.size.width/2.0f,self.frame.size.height/2.0f)];
                [self addSubview:activity];
            }
            else {
                [activity setCenter:CGPointMake(self.frame.size.width/2.0f,self.frame.size.height/2.0f)];
            }
            [activity startAnimating];
    
        } completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, NSURL *imageURL) {
            UIActivityIndicatorView *activity = (UIActivityIndicatorView *)[self viewWithTag:100000];
            if ([activity isKindOfClass:[UIActivityIndicatorView class]]) {
                [activity stopAnimating];
            }
        }];
    }
    

    This will add UIActivityIndicatorView to the center of UIImageView when image downloading in progress and remove on completion.

    0 讨论(0)
  • 2021-01-30 09:38

    Same code as above in SWIFT 4:

    let activityIndicator = UIActivityIndicatorView.init(activityIndicatorStyle: UIActivityIndicatorViewStyle.gray)
        activityIndicator.center = addImage.center
        activityIndicator.hidesWhenStopped = true
    
        addImage.sd_setImage(with: URL(string: feed.image), completed: { (image: UIImage?, error: Error?, cacheType: SDImageCacheType, imageURL: URL?) in
            activityIndicator.removeFromSuperview()
        })
    
        addImage.addSubview(activityIndicator)
        activityIndicator.startAnimating()
    
    0 讨论(0)
  • 2021-01-30 09:40

    SDWebImage has a built in Acitvity Indicator that works perfectly. Try this:

    Updates: SWIFT 5 SDWebImage 5.x.x

            imgView.sd_imageIndicator = SDWebImageActivityIndicator.gray
            imgView.sd_setImage(with: url, placeholderImage: UIImage(named: "placeholder"))
    

    Swift 3:

    imgView.setShowActivityIndicator(true)
    imgView.setIndicatorStyle(.gray)
    imgView.sd_setImage(with: URL(string: urlString), placeholderImage: UIImage(named: "placeholder"))
    
    0 讨论(0)
  • 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 <UIKit/UIKit.h>
    
    #import <SDWebImage/UIImageView+WebCache.h>
    
    @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

    0 讨论(0)
提交回复
热议问题