【问题标题】:AFNetworking+UIImageView placeholder image shows up, but not URL image显示 AFNetworking+UIImageView 占位符图像,但不显示 URL 图像
【发布时间】:2012-02-27 03:31:48
【问题描述】:

我正在尝试使用 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];

【问题讨论】:

  • 你的图片在 UITableViewCells 中吗?您是否在 cellForRowAtIndexPath: 委托方法中调用 setImageWithURL
  • 是的,你的两个问题。
  • 能否在tableView:cellForRowAtIndexPath:中包含用于设置远程图像的代码?
  • Mattt,包含该代码。它在我的帖子的顶部。

标签: ios ios5 uiimageview afnetworking


【解决方案1】:

原来我请求图像的服务器正在发送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];
}

它解决了我的问题。

【讨论】:

  • 你成就了我的一天!对于 AFNetworking 2.0,它是 AFURLResponseSerialization.m 中的 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-bitmap", nil];
  • 我应该如何使用这个? ,我想也有同样的问题! LINK
【解决方案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 而无需更改库。

【讨论】:

    【解决方案3】:

    AFNetworking 默认不支持 image/jpg MIME TYPE

    您无需修改​​ AFNetworking 库即可支持它

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

    【讨论】:

      【解决方案4】:

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

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

      【讨论】:

      • 好的建议 Jonkroll,但对我的事业没有帮助。
      • FWIW,所有 AFNetworking 回调块都默认在主线程上执行。
      【解决方案5】:

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2019-04-11
        • 1970-01-01
        • 2014-11-29
        • 1970-01-01
        • 1970-01-01
        • 2023-04-05
        相关资源
        最近更新 更多