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
I believe with the latest version of sdwebimage this is a better method.
[self.customImageView setImageWithURL:imageURL
placeholderImage:nil
options:nil
progress:^(NSUInteger receivedSize, long long expectedSize) { [self.activityIndicator startAnimating]; }
completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType) { [self.activityIndicator stopAnimating]; }];
Note: If you don't have a property setup obviously self.activityindicator won't work.
I would use Michael Frederick's example to create the activityindicator instead.
That's my solution. In that file : UIImageView+WebCache.m
Find that method and change like that:
- (void)setImageWithURL:(NSURL *)url placeholderImage:(UIImage *)placeholder
{
UIActivityIndicatorView *activity = [[UIActivityIndicatorView alloc]initWithActivityIndicatorStyle:(UIActivityIndicatorViewStyleGray)];
[activity startAnimating];
[activity setFrame:CGRectMake(self.frame.origin.x - 20, self.frame.origin.y - 10, self.frame.size.width, self.frame.size.height)];
[self addSubview:activity];
//[self setImageWithURL:url placeholderImage:placeholder options:0 progress:nil completed:nil];
[self setImageWithURL:url
placeholderImage:placeholder
options:0
completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType)
{
[activity removeFromSuperview];
}];
}
The above code is slightly wrong. The line 'activityIndicator.center = imageView.center' does not give you the correct coordinates to center the progress view. For me, it was showing the indicator below the cell.
Note - I am using the latest version of the SDWebImage code so the method is slightly different. I am also using a UIButton for the imageView
Try this:
NSURL *url = [NSURL URLWithString:@"www.someimageurl.com"];
__block UIActivityIndicatorView *activityIndicator = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray];
activityIndicator.frame = cell.imageView.bounds;
[cell.imageView addSubview:activityIndicator];
[activityIndicator startAnimating];
[cell.imageView setImageWithURL:url forState:UIControlStateNormal completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType) {
[activityIndicator removeFromSuperview];
}];
Another solution is to use an animated image as the placeholder image by using:
[UIImage animatedImageNamed:@"animated_placeholder" duration:1]
This fork has support for this functionality. Take a look at the diff.
But, in general, you can do it rather easily without using that fork:
__block UIActivityIndicatorView *activityIndicator = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:activityStyle];
activityIndicator.center = imageView.center;
activityIndicator.hidesWhenStopped = YES;
[imageView setImageWithURL:[NSURL URLWithString:@"http://www.domain.com/path/to/image.jpg"]
placeholderImage:[UIImage imageNamed:@"placeholder.png"]
success:^(UIImage *image) { [activityIndicator removeFromSuperview]; }
failure:^(NSError *error) { [activityIndicator removeFromSuperview]; }];
[imageView addSubview:activityIndicator];
[activityIndicator startAnimating];
My list is quite lag when the user scrolling the list up and down repeatedly. I found out that the activityindicator had been added infinitely when the cellForItemAtIndexPath is being repeatedly called.
To solve this, add a tag at your activityindicator:
activityIndicator.tag = 9999;
Before the UIActivityIndicator is being created, add these lines to remove the previous activityindicator before adding the new one:
UIView* toDeleteLoader = [cell viewWithTag:9999];
if(toDeleteLoader != nil){
[toDeleteLoader removeFromSuperview];
}
Note: tag content can be any numbers that it won't be used in your cell.