AFNetworking setImageWithURLRequest not setting remote image

Eric picture Eric · Nov 12, 2012 · Viewed 13.3k times · Source

I'm using AFNetworking's UIImageView+AFNetworking.h to asynchronously load an online image onto a UIImageView.

The remote image loads very well when I use setImageWithURLRequest:, but when I try using setImageWithURLRequest:placeholderImage:success:failure the remote image is downloaded but the UIImageView does not set it as its UIImage.

Here's my code:

@property (strong) IBOutlet UIImageView *imageView;

-(void)viewDidAppear:(BOOL)animated{
    [super viewDidAppear:animated];

    [_imageView setImageWithURLRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:@"http://i.imgur.com/fVhhR.png"]]
                       placeholderImage:nil
                                success:^(NSURLRequest *request , NSHTTPURLResponse *response , UIImage *image ){
                                    NSLog(@"Loaded successfully: %d", [response statusCode]);
                                }
                                failure:^(NSURLRequest *request, NSHTTPURLResponse *response, NSError *error){
                                    NSLog(@"failed loading: %@", error);
                                }
     ];
}

Image always loads successfully, the status code is always 200. But I have to add [_imageView setImage:image]; to the success block for the UIImageView to set the image.

I'm using iOS 6.0

Any ideas?

Answer

Robin Summerhill picture Robin Summerhill · Nov 12, 2012

If you supply a success block then you are responsible for setting the image property of your UIImageView. If you supply a nil success block then AFNetworking does it for you.

This is the relevant section of UIImageView+AFNetworking.m (line 117):

if (success) {
    success(operation.request, operation.response, responseObject);
} else {
    self.image = responseObject;
}

Alternatively, just use the -setImageWithURL:placeholderImage: without the completion blocks.