7

我正在尝试使用 AFNetworking UIImageView 调用从 URL 加载图像,如下所示:

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

占位符图像始终显示,但来自“feed.imageURL”的实际图像永远不会出现。我已验证该 URL 实际上是正确的。我什至硬编码它以确保,但仍然没有。

我的基本应用程序设置是一个选项卡控制器……在 viewDidLoad 中,我调用了一个方法“fetchFeed”,它执行 HTTP 请求以收集我的 JSON 数据。

我的请求块看起来像:

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];
4

5 回答 5

9

原来我请求图像的服务器正在发送content-type "image/jpg",默认情况下 AFNetworking 不支持这种文件类型。

我将 AFImageRequestOperation 中的类方法更改为:

+ (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];
}

它解决了我的问题。

于 2012-02-27T23:57:58.063 回答
2

您可以通过这个库来设法接受content-type您想要的内容,只需更改request如下:

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

并调用 AFNetworking 方法:

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

这样,您将能够在content-type 更改库的情况下覆盖 。

于 2013-06-26T16:46:47.347 回答
2

AFNetworking 默认不支持image/jpg MIME TYPE

无需修改 AFNetworking 库即可支持

[AFImageRequestOperation addAcceptableContentType:@"image/jpg"];
于 2013-12-12T03:35:28.193 回答
0

所有操纵 UI 的操作都必须在主线程上执行。因此,在完成块中重新加载 tableview 数据时,您可能需要使用“performSelectorOnMainThread:”。

[self.tableView performSelectorOnMainThread:@selector(reloadData) withObject:nil waitUntilDone:NO]
于 2012-02-27T04:06:17.893 回答
0

我遇到了类似的问题,但结果证明我传递了一个包含空格的 URL。当我使用图像正确编码 URL 时stringByAddingPercentEscapesUsingEncoding:,现在加载。

于 2013-01-31T23:56:43.930 回答