質問

I'm 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 always shows up, but the actual image from "feed.imageURL" never does. I've verified that the URL is actually correct. I even hardcoded it to make sure, and still nothing.

My basic app setup is a tab controller...and in viewDidLoad, I call a method "fetchFeed" which performs the HTTP request to gather my JSON data.

My request block looks like:

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];
役に立ちましたか?

解決

Turns out the server I was requesting the image from was sending content-type "image/jpg" and by default AFNetworking does not support this file type.

I changed the class method in AFImageRequestOperation to look like:

+ (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 it fixed my problem.

他のヒント

You can manage to accept what content-type you want with this library simply changing the request like this:

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 will be able to override the content-type without changing the library.

AFNetworking doesn't support image/jpg MIME TYPE by default.

You can support it without modifying the AFNetworking Library

[AFImageRequestOperation addAcceptableContentType:@"image/jpg"];

All operations that manipulate the UI must be performed on the main thread. So you may need to use 'performSelectorOnMainThread:' when reloading your tableview data in the completion block.

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

I had a similar problem but it turned out that I was passing a URL which contained spaces in it. When I properly encoded the URL using stringByAddingPercentEscapesUsingEncoding: the images now load.

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top