【问题标题】:How to use success & failure block in SDwebimage如何在 SDwebimage 中使用成功和失败块
【发布时间】:2014-03-11 09:02:06
【问题描述】:

我有最新版本的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 获取图像时出现错误,我想处理

【问题讨论】:

    标签: ios iphone ios6 ios7 sdwebimage


    【解决方案1】:

    试试这个:

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

    【讨论】:

      【解决方案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;
                                      }
      }];
      

      希望你们觉得这很有用。

      【讨论】:

        【解决方案3】:
                    imageView.sd_setImageWithURL(NSURL(string: urlString), completed: {
                        (image, error, cacheType, url) in
                        // do your custom logic here
                    })
        

        swift 2.0 的示例代码

        【讨论】:

          【解决方案4】:

          它有完成块

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

          你可以检查error是否为nil,那么一切都很好。

          【讨论】:

          • 是的,这就是我现在正在做的事情
          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2017-11-24
          相关资源
          最近更新 更多