AFNetworking/AFNetworking — Issue #183: UIImageView+AFNetworking failure block
Author: erikolsson
Created: 2012-01-20 09:05:23+00:00
State: open

When loading a critical image resource, it's not possible to try again with the same URL since the af_imageRequestOperation is not nil.

Consider this code:

- (void)trySetImage {
NSString *url = @"https://a248.e.akamai.net/assets.github.com/img/adfc0bef364ef82cd0a2bf395ed02fe56619d376/687474703a2f2f656e67696e656572696e672e676f77616c6c612e636f6d2f41464e6574776f726b696e672f61666e6574776f726b696e672d6c6f676f2e706e67";

NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:url]
cachePolicy:NSURLRequestUseProtocolCachePolicy
timeoutInterval:30.0];
[request setHTTPShouldHandleCookies:NO];
[request setHTTPShouldUsePipelining:YES];
[self.imageView setImageWithURLRequest:request
placeholderImage:nil
success:^(NSURLRequest *_request, NSHTTPURLResponse *response, UIImage *image) {
} failure:^(NSURLRequest *_request, NSHTTPURLResponse *response, NSError *error) {
[self trySetImage];
}];
}

trySetImage will only run once in case the image resource fails to load.