【问题标题】:Set TableView Cell image from AFNetworking download从 AFNetworking 下载设置 TableView Cell 图像
【发布时间】:2014-03-21 15:04:11
【问题描述】:

我正在使用 AFNetworking 下载设置为表格视图的 rss 提要图像。以下是我正在使用的代码。

- (UITableViewCell *)tableView:(UITableview *)tableView cellForItemAtIndexPath:(NSIndexPath *)indexPath;
{
    Cell *cell = [cv dequeueReusableCellWithReuseIdentifier:@"MY_CELL" forIndexPath:indexPath];

    NSString *wallpaperLink = [af setThumbString:[[feeds objectAtIndex:indexPath.row] objectForKey: @"wallpaper"]];

    //AfNetwork Download
    AFHTTPRequestOperation *requestOperation = [[AFHTTPRequestOperation alloc] initWithRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:wallpaperLink]]];
    requestOperation.responseSerializer = [AFImageResponseSerializer serializer];
    [requestOperation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) {
       //Set Cell Image from response
       cell.imageView.image = responseObject;
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        NSLog(@"Image error: %@", error);
    }];
    [requestOperation start];

    return cell;
}

但是当我阅读一些教程时,我意识到这不是使用 AFNetworking 下载异步图像的方法。谁能告诉我我的代码,如何下载和添加responseobjectcell.imageView.image ???

【问题讨论】:

    标签: ios objective-c uitableview afnetworking


    【解决方案1】:

    我这样做是为了避免内存泄漏:

    NSURL *url = [NSURL URLWithString:@"http:url..."];
    NSURLRequest *request = [NSURLRequest requestWithURL:url];
    UIImage *placeholderImage = [UIImage imageNamed:@"your_placeholder"];
    
    __weak UITableViewCell *weakCell = cell;
    
    [cell.imageView setImageWithURLRequest:request
                          placeholderImage:placeholderImage
                                   success:^(NSURLRequest *request, NSHTTPURLResponse *response, UIImage *image) {
    
                                       weakCell.imageView.image = image;
                                       [weakCell setNeedsLayout];
    
                                   } failure:nil];
    

    它适用于 AFNetworking 2。

    正如@Greg 在评论中建议的那样: 您必须添加#import "UIImageView+AFNetworking.h".

    【讨论】:

    • No visible @interface for 'UIImageView' 声明选择器 'setImageWithURLRequest:placeholderImage:success:failure:' 这个错误来找我
    • 你必须添加#import "UIImageView+AFNetworking.h"。
    【解决方案2】:

    如果你正在使用 AFNetworking,为什么不使用 UIImageView+AFNetworking 类别呢?只需导入 UIImageView+AFNetworking.h 并使用此方法:

    - (void)setImageWithURLRequest:(NSURLRequest *)urlRequest
                  placeholderImage:(UIImage *)placeholderImage
                           success:(void (^)(NSURLRequest *request, NSHTTPURLResponse *response, UIImage *image))success
                           failure:(void (^)(NSURLRequest *request, NSHTTPURLResponse *response, NSError *error))failure;
    

    编辑:设置图片后不要忘记在单元格上调用setNeedsLayout

    【讨论】:

    • 我无法添加 UIImageView+AFNetworking.h。 UIImageView+AFNetworking.h 文件未找到错误出现
    • 如果您克隆了 AFNetworking 存储库 (github.com/AFNetworking/AFNetworking) 的副本,您将在“UIKit+AFNetworking”文件夹中找到该类和其他 UIKit 类别。如果您需要帮助找到它,请告诉我。您只需将“UIImageView+AFNetworking.h”拖入您的项目中即可。
    【解决方案3】:

    在进行异步图片下载时,无法立即在UITableViewCell 上获取结果图片。所以,你应该下载图像并使用下载图像数组显示它们,按照tableViewindexPath

    请参考此link 使用延迟加载下载图像并将其显示在UITableViewCell

    谢谢!

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-11-01
      • 1970-01-01
      • 1970-01-01
      • 2020-06-03
      • 1970-01-01
      相关资源
      最近更新 更多