显示AFNetworking + UIImageView占位符图像,但不显示URL图像

Ada*_*son 7 uiimageview ios ios5 afnetworking

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

[self.image setImageWithURL:[NSURL URLWithString:feed.imageURL] placeholderImage:     [UIImage imageNamed:@"logo"]];
Run Code Online (Sandbox Code Playgroud)

占位符图像始终显示,但"feed.imageURL"中的实际图像永远不会显示.我已经验证了网址实际上是正确的.我甚至硬编码确保,但仍然没有.

我的基本应用程序设置是一个选项卡控制器...在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];
Run Code Online (Sandbox Code Playgroud)

Ada*_*son 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];
}
Run Code Online (Sandbox Code Playgroud)

它解决了我的问题.

  • 你让我今天一整天都感觉很好!对于AFNetworking 2.0,它是`self.acceptableContentTypes = [[NSSet alloc] initWithObjects:@"image/tiff",@"image/jpeg",@"image/jpg",@"image/gif",@"image/png" ,@"image/ico",@"image/x-icon",@"image/bmp",@"image/x-bmp",@"image/x-xbitmap",@"image/x-win-位图",nil];`在AFURLResponseSerialization.m中 (2认同)