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?
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.
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.
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
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?
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With