Show activity indicator in SDWebImage

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

    SDWebImage 5.0 has support of displaying activity indicator using SDWebImageActivityIndicator class.

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

    This is how you do it:

    [imageView setIndicatorStyle:UIActivityIndicatorViewStyleWhite];
    [imageView setShowActivityIndicatorView:true];
    [imageView sd_setImageWithURL:[NSURL URLWithString:imageUrl] placeholderImage:[UIImage imageNamed:@"defaultl_image"]];

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

    Best solution that worked for me is below. It is beautiful and encouraging to show progress indicator when image is downloading. I used UICircularSlider as rounded progress view. You can also try it.

        [_imgPost sd_setImageWithURL:urlPost placeholderImage:zeroImage options:SDWebImageContinueInBackground progress:^(NSInteger receivedSize, NSInteger expectedSize)
        {
            CGFloat domandeFloat = [[NSNumber numberWithInteger: receivedSize] floatValue];
            CGFloat corretteFloat = [[NSNumber numberWithInteger: expectedSize] floatValue];
    
    
            float currentProgress = (domandeFloat/corretteFloat)*100;
    
            NSLog(@"progress %.f%%",currentProgress);
            DBG(@"%li, %li", receivedSize, expectedSize);
            [_progress setValue:currentProgress];
        } completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, NSURL *imageURL) {
    
            [_progress setHidden:YES];
        }];
    

    Here _progress is an instance of UICircularSlider

    0 讨论(0)
  • 2021-01-30 09:43
    [cell.dreamImageView setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@",dream.thumbnail]] placeholderImage:[UIImage imageNamed:@"dream_bg_2.png"] options:0 progress:^(NSInteger receivedSize, NSInteger expectedSize) {
    
        CGFloat domandeFloat = [[NSNumber numberWithInt: receivedSize] floatValue];
        CGFloat corretteFloat = [[NSNumber numberWithInt: expectedSize] floatValue];
    
    
        float currentProgress = domandeFloat/corretteFloat;
    
         NSLog(@"progress %f",currentProgress);
        cell.progressView.progress = currentProgress;
    
        //[self.dreamsTableView reloadData];
    
    
    } completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType) {
    
        cell.progressView.hidden = YES;
    }];
    
    0 讨论(0)
  • 2021-01-30 09:43

    Solution updated for Swift 2.0 for using SDWebImage to load image URL

    imageView.setShowActivityIndicatorView(true)
    
    imageView.setIndicatorStyle(.White)
    
    0 讨论(0)
  • 2021-01-30 09:44

    Last solution

    You can download UIActivityIndicator-for-SDWebImage, which is easiest way to add a UIActivityView to your SDWebImage view. Using CocoaPods, just add this line to your podfile:

    pod 'UIActivityIndicator-for-SDWebImage'
    

    You can use one of these lines depending on your preferences:

    - (void)setImageWithURL:(NSURL *)url usingActivityIndicatorStyle:(UIActivityIndicatorViewStyle)activityStyle;
    - (void)setImageWithURL:(NSURL *)url placeholderImage:(UIImage *)placeholder usingActivityIndicatorStyle:(UIActivityIndicatorViewStyle)activityStyle;
    - (void)setImageWithURL:(NSURL *)url placeholderImage:(UIImage *)placeholder options:(SDWebImageOptions)options usingActivityIndicatorStyle:(UIActivityIndicatorViewStyle)activityStyle;
    - (void)setImageWithURL:(NSURL *)url completed:(SDWebImageCompletionBlock)completedBlock usingActivityIndicatorStyle:(UIActivityIndicatorViewStyle)activityStyle;
    - (void)setImageWithURL:(NSURL *)url placeholderImage:(UIImage *)placeholder completed:(SDWebImageCompletionBlock)completedBlock usingActivityIndicatorStyle:(UIActivityIndicatorViewStyle)activityStyle;
    - (void)setImageWithURL:(NSURL *)url placeholderImage:(UIImage *)placeholder options:(SDWebImageOptions)options completed:(SDWebImageCompletionBlock)completedBlock usingActivityIndicatorStyle:(UIActivityIndicatorViewStyle)activityStyle;
    - (void)setImageWithURL:(NSURL *)url placeholderImage:(UIImage *)placeholder options:(SDWebImageOptions)options progress:(SDWebImageDownloaderProgressBlock)progressBlock completed:(SDWebImageCompletionBlock)completedBlock usingActivityIndicatorStyle:(UIActivityIndicatorViewStyle)activityStyle;
    

    Example of Use

    Just import

    #import <UIActivityIndicator-for-SDWebImage/UIImageView+UIActivityIndicatorForSDWebImage.h>
    

    and use this code

    [imageView setImageWithURL:[NSURL URLWithString:@"https://media.licdn.com/mpr/mpr/wc_200_200/p/1/005/07f/0a3/30cb8dd.jpg"] placeholderImage:[UIImage imageNamed:@"myImage.jpg"] usingActivityIndicatorStyle:UIActivityIndicatorViewStyleGray];
    
    0 讨论(0)
提交回复
热议问题