Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

AFNetworking setImageWithURLRequest not setting remote image

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?

like image 541
Eric Avatar asked Nov 12 '12 16:11

Eric


4 Answers

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.

like image 130
Robin Summerhill Avatar answered Nov 10 '22 23:11

Robin Summerhill


I had the same problem and after being frustrated with the docs for the library I found what Robin mentioned above. So now I'm using:

[imageView setImageWithURLRequest:imageRequest placeholderImage:nil success:nil failure:^(NSURLRequest *request, NSHTTPURLResponse *response, NSError *error) {
    NSLog(@"Unable to retrieve image");
}];

Works like a charm. I hope this helps.

like image 35
zquintana Avatar answered Nov 11 '22 01:11

zquintana


The doc is very clear:

If a success block is specified, it is the responsibility of the block to set the image of the image view before returning. If no success block is specified, the default behavior of setting the image with self.image = image is applied.

So you can either pass a nil block for success (as mentioned above) or call setImage:image yourself if you specify a block

like image 37
Erwan Avatar answered Nov 11 '22 01:11

Erwan


In your success block, you need to give the UIImaage *image to your UIImageView. I assuming you are getting the NSLog(@"Loaded successfully: %d", [response statusCode]);

Also, it appears your placeholder image is nil. Don't you want a placeholder like a loading image in there?

like image 1
false_memories Avatar answered Nov 11 '22 00:11

false_memories