Question

I have latest version of SDWebimage but it doesnt have Success & failure I tried the following method but SDwebimage doesnt have method

[self.imageView setImageWithURL:[NSURL URLWithString:self.imageURL]
              placeholderImage:[UIImage imageNamed:@"YourPlaceholder.png"]
                       success:^(UIImage *image) {
                           // remove animation

                       }
                       failure:^(NSError *error) {
                           NSLog(@"thumbnail error: %@",error);
                           // handle failed download

                       }];

Does anybody know how to add success & failure block in SDwebimage setImageWithURL or any other alternatife I want to handle if there is some error while getting image from URL

Was it helpful?

Solution

Try this:

[self.imageView setImageWithURL:[NSURL URLWithString:imageURL]
                   placeholderImage:[UIImage imageNamed:@"YourPlaceholder.png"]
                          completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType) {
                              //... completion code here ...
                          }];

OTHER TIPS

Solution for Swift 3 :

cell.imageView?.sd_setImage(with: url) { (image, error, cache, urls) in
            if (error != nil) {
                //Failure code here
                cell.imageView.image = UIImage(named: "ico_placeholder")
            } else {
                //Success code here
                cell.imageView.image = image
            }
}

Solution for Objective C :

[cell.imageView sd_setImageWithURL:url
                  placeholderImage:nil
                         completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, NSURL *imageURL) {
                                if (error) {
                                  //Failure code here
                                  self.imageView.image = [UIImage imageNamed:@"ico_placeholder"];
                                } else {
                                  //Success code here
                                  self.imageView.image = image;
                                }
}];

Hope you guys find this useful.

            imageView.sd_setImageWithURL(NSURL(string: urlString), completed: {
                (image, error, cacheType, url) in
                // do your custom logic here
            })

sample code for swift 2.0

it has completion block

completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType)

You can check if error is nil, then everything is fine.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top