Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

AFNetworing - setImageWithURLRequest not working

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.

like image 668
Anders Avatar asked May 04 '13 07:05

Anders


2 Answers

I see three problems:

  1. -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?

  2. More recent versions of AFNetworking require you to set the image yourself if you're defining a success block.

  3. 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);
}];
like image 192
Andreas Ley Avatar answered Sep 29 '22 03:09

Andreas Ley


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:@""];
like image 25
icodebuster Avatar answered Sep 29 '22 05:09

icodebuster