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
SDWebImage 5.0 has support of displaying activity indicator using SDWebImageActivityIndicator class.
let imageView = UIImageView()
imageView.sd_imageIndicator = SDWebImageActivityIndicator.gray
This is how you do it:
[imageView setIndicatorStyle:UIActivityIndicatorViewStyleWhite];
[imageView setShowActivityIndicatorView:true];
[imageView sd_setImageWithURL:[NSURL URLWithString:imageUrl] placeholderImage:[UIImage imageNamed:@"defaultl_image"]];
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
[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;
}];
Solution updated for Swift 2.0 for using SDWebImage to load image URL
imageView.setShowActivityIndicatorView(true)
imageView.setIndicatorStyle(.White)
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;
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];