下载失败时如何让AFNetworking设置默认图片?

How to make AFNetworking set a default image when the download fails?

我想执行以下操作:

  1. 当图像正在加载时 => 必须显示微调框或其他图像以指示正在加载;
  2. 加载图片时=>必须显示图片;
  3. 当图像失败时 => 必须显示静态 "no image available" 图像。

我试过:

- (void)setImageWithURL:(NSURL *)url
   placeholderImage:(UIImage *)placeholderImage

但是我不知道如何处理失败事件。

你为什么不用

setImageWithURLRequest:placeholderImage:success:failure:

From the doc

并在失败块中设置想要的占位符图像?

示例:

NSURLRequest * aURLRequest = [[NSURLRequest alloc] initWithURL:[[NSURL alloc] initWithString: @"A-URL"]];
UIImageView * img = [[UIImageView alloc] init];
__weak UIImageView* weakImg = img;
[img setImageWithURLRequest:aURLRequest
           placeholderImage:nil
                    success:^(NSURLRequest *request, NSHTTPURLResponse *response, UIImage *image) {
                        //default
                    }
                    failure:^(NSURLRequest *request, NSHTTPURLResponse *response, NSError *error) {
                        weakImg.image = [UIImage imageNamed:@"fallbackImage"];
                    }];