Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use success & failure block in SDwebimage

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

like image 950
DAMM108 Avatar asked Mar 11 '14 09:03

DAMM108


3 Answers

Try this:

[self.imageView setImageWithURL:[NSURL URLWithString:imageURL]
                   placeholderImage:[UIImage imageNamed:@"YourPlaceholder.png"]
                          completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType) {
                              //... completion code here ...
                          }];
like image 140
EnriMR Avatar answered Oct 18 '22 22:10

EnriMR


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.

like image 26
Ankit Kumar Gupta Avatar answered Oct 19 '22 00:10

Ankit Kumar Gupta


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

sample code for swift 2.0

like image 1
Nik Yekimov Avatar answered Oct 18 '22 23:10

Nik Yekimov