0

I'm using SDWebImage library for caching image in my iOS app. Now when loading image from server I'm showing an activity indicator on the imageview. I'm using the following code

__block UIActivityIndicatorView *activityIndicator = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray];
  activityIndicator.center = self.serviceImageView.center;
  activityIndicator.hidesWhenStopped = YES;
  [activityIndicator startAnimating];
  [self.serviceImageView addSubview:activityIndicator];
  [self.serviceImageView setImageWithURL:[NSURL URLWithString:[self.service valueForKey:@"image"]] placeholderImage:nil completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType) {
    [activityIndicator removeFromSuperview];
  }];

It works fine generally. But for some imageurls it didn't load the image and so it never comes to completed block & the activity indicator is spinning infinite. So is there any way to set a timeout so that after a certain amount of time it gives me an error or something like that so I can stop the activity indicator.

It's bit argent. Please help.

Walid Hossain
  • 2,724
  • 2
  • 28
  • 39

1 Answers1

0

You should check if the URL is valid, because when you pass nil to setImageWithURL the completion block won't be called.

NSURL* url = [NSURL URLWithString:[self.service valueForKey:@"image"]];

if (url)
  [self.serviceImageView setImageWithURL:url placeholderImage:nil completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType) {
    [activityIndicator removeFromSuperview];
  }];
I
Felix
  • 35,354
  • 13
  • 96
  • 143
  • The url was ok, But that was not returning any image And I just see that It works for the first time fine. After that the SDWebImage blacklisted the url and so it was not calling completion block. I had to use option SDWebImageRetryFailed – Walid Hossain Apr 26 '13 at 12:05