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
Try this:
[self.imageView setImageWithURL:[NSURL URLWithString:imageURL]
placeholderImage:[UIImage imageNamed:@"YourPlaceholder.png"]
completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType) {
//... completion code here ...
}];
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With