The AFNetworking + UIImageView placeholder image will be displayed, but not the URL

I am trying to use the AFNetworking UIImageView call to load images from a URL, as shown below:

[self.image setImageWithURL:[NSURL URLWithString:feed.imageURL] placeholderImage: [UIImage imageNamed:@"logo"]]; 

The placeholder image is always displayed, but the actual image from the feed.imageURL file is never executed. I checked that the url is really correct. I even hardcoded it to make sure, and still nothing.

My main application setup is a tab controller ... and in viewDidLoad I call the "fetchFeed" method, which makes an HTTP request to collect my JSON data.

My request block looks like this:

 AFJSONRequestOperation *operation = [AFJSONRequestOperation JSONRequestOperationWithRequest:request success:^(NSURLRequest *request, NSHTTPURLResponse *response, id JSON) { [self parseDictionary:JSON]; isLoading = NO; [self.tableView reloadData]; } failure:^(NSURLRequest *request, NSHTTPURLResponse *response, NSError *error, id JSON) { NSLog(@"Error: %@", error); [self showNetworkError]; isLoading = NO; [self.tableView reloadData]; }]; operation.acceptableContentTypes = [NSSet setWithObjects:@"application/json", @"text/json", @"text/javascript", @"text/html", nil]; [queue addOperation:operation]; 
+7
source share
5 answers

It turns out that the server to which I requested the image sent the content-type "image/jpg" , and by default AFNetworking does not support this file type.

I changed the class method in AFImageRequestOperation to look like this:

 + (NSSet *)defaultAcceptableContentTypes { return [NSSet setWithObjects:@"image/tiff", @"image/jpeg", @"image/gif", @"image/png", @"image/ico", @"image/x-icon" @"image/bmp", @"image/x-bmp", @"image/x-xbitmap", @"image/x-win-bitmap", @"image/jpg", nil]; } 

and he fixed my problem.

+9
source

You can accept what content-type you want using this library by simply changing the request as follows:

 NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:yourURL]; [request addValue:@"image/*" forHTTPHeaderField:@"Accept"]; 

And call the AFNetworking method:

 AFJSONRequestOperation *operation = [AFJSONRequestOperation JSONRequestOperationWithRequest:request success:^(NSURLRequest *request, NSHTTPURLResponse *response, id JSON) { } failure:^(NSURLRequest *request, NSHTTPURLResponse *response, NSError *error, id JSON) { }]; 

This way you can override the content-type without changing the library.

+2
source

AFNetworking does not support image / jpg MIME TYPE by default.

You can support it without changing the AFNetworking library.

 [AFImageRequestOperation addAcceptableContentType:@"image/jpg"]; 
+2
source

All operations that control the user interface must be performed in the main thread. Therefore, when reloading the table view data in the completion block, you may need to use "performSelectorOnMainThread:".

 [self.tableView performSelectorOnMainThread:@selector(reloadData) withObject:nil waitUntilDone:NO] 
0
source

I had a similar problem, but it turned out that I was passing a URL containing spaces in it. When I correctly encoded the URL using stringByAddingPercentEscapesUsingEncoding: images are now loaded.

0
source

All Articles