I want to set tableview cell images in one of my apps. I use AFNetworking (1.2.1)
and I do it with setImageWithURLRequest
.
My code in which I set images looks like this:
if (user.avatar) {
NSString *imageUrl = user.avatar.url;
[cell.profileImage setImageWithURLRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:imageUrl]] placeholderImage:nil success:^(NSURLRequest *request, NSHTTPURLResponse *response, UIImage *image) {
[cell setNeedsLayout];
} failure:^(NSURLRequest *request, NSHTTPURLResponse *response, NSError *error) {
NSLog(@"Error: %@", error);
}];
}
My error log returns this:
Error: Error Domain=NSURLErrorDomain Code=-999 "The operation couldn’t be completed. (NSURLErrorDomain error -999.)" UserInfo=0x1f5b56a0 {NSErrorFailingURLKey=https://d2rfichhc2fb9n.cloudfront.net/image/5/Cf7T_BA6- NFGL5ah0w4hdvmk_Vp7InMiOiJzMyIsImIiOiJhZG4tdXNlci1hc3NldHMiLCJrIjoiYXNzZXRzL3VzZXIvZTIvM2MvMjA vZTIzYzIwMDAwMDAwMDAwMC5qcGciLCJvIjoiIn0}
And the images dosn't get set. I didn't have this problem before I updated to AFNetworking (1.2.1)
. Any ideas?
Update
Tried to strip the whitespace from the url like this:
NSString *imageUrl = [user.avatar.url stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
But I get the same error. And it could maybe be dangerous if the API sometime would contain a image file name that contains whitespaces.
I see three problems:
-999 means NSURLErrorCancelled
, so it looks like the URLRequest
gets cancelled somehow. Where is this code of yours? Is it possible that the cell
object or the UIImageView
profileImage
is released prematurely?
More recent versions of AFNetworking require you to set the image yourself if you're defining a success block.
Make sure the URL is valid.
Try this (works when placed in tableView:cellForRowAtIndexPath:
):
__weak UITableViewCell *weakCell = cell;
[cell.imageView setImageWithURLRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:imageUrl]] placeholderImage:nil success:^(NSURLRequest *request, NSHTTPURLResponse *response, UIImage *image) {
if (weakCell)
{
weakCell.imageView.image = image;
[weakCell setNeedsLayout];
}
} failure:^(NSURLRequest *request, NSHTTPURLResponse *response, NSError *error) {
NSLog(@"Error: %@", error);
}];
There seem to be some extra space in your url
Your url is https://d2rfichhc2fb9n.cloudfront.net/image/5/Cf7T_BA6- NFGL5ah0w4hdvmk_Vp7InMiOiJzMyIsImIiOiJhZG4tdXNlci1hc3NldHMiLCJrIjoiYXNzZXRzL3VzZXIvZTIvM2MvMjA vZTIzYzIwMDAwMDAwMDAwMC5qcGciLCJvIjoiIn0
Url Without Space
Because of the extra space the image url is invalid and its not getting fetched.
EDIT
NSString *strCorrectUrl = [user.avatar.url stringByReplacingOccurrencesOfString:@" "
withString:@""];
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