如何使用成功& SDwebimage中的失败块

时间:2014-03-11 09:02:06

标签: ios iphone ios6 ios7 sdwebimage

我有SDWebimage的最新版本,但它没有成功&失败 我尝试了以下方法,但SDwebimage没有方法

[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

                       }];

有人知道如何增加成功吗? SDwebimage setImageWithURL中的失败阻止或任何其他替换 如果从URL

获取图像时出现错误,我想处理

4 个答案:

答案 0 :(得分:7)

试试这个:

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

答案 1 :(得分:2)

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
            }
}

目标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;
                                }
}];

希望你们觉得这很有用。

答案 2 :(得分:1)

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

swift 2.0的示例代码

答案 3 :(得分:0)

它有完成块

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

你可以检查错误是否为零,然后一切都很好。