【问题标题】:AFNetworkings setImageWithURLRequest displays wrong image for UITableViewCellAFNetworkings setImageWithURLRequest 为 UITableViewCell 显示错误的图像
【发布时间】:2014-08-28 06:45:36
【问题描述】:

以下代码用于我的tableView:cellForRowAtIndexPath 方法。 我使用 AFNetworking 的setImageWithURLRequest 来显示单元格的图像。 但是,有时会在我的 tableView 中的单元格上放置错误的图像。

我认为错误是触发成功块并且contact 对象不再对应于下载的图像。

// ... init/deque ...
/* CAN BE EITHER USER OR CONTACT DETAILS */
id contact = [[self.connections objectAtIndex:indexPath.section] objectAtIndex:indexPath.row];

if ([contact isKindOfClass:[User class]]) {
    [cell configurateCellWithUser:(User *)contact];
    if(((User*)contact).avatar_name.length > 0){
        [cell.profilePicture setImageWithURLRequest:[self getURLRequestForUser:contact]
                                   placeholderImage:[UIImage imageWithData:((User*)contact).avatar_image]
                                            success:^(NSURLRequest *request, NSHTTPURLResponse *response, UIImage *image) {

                                                cell.profilePicture.image = image;                                                       
                                                ((User*)contact).avatar_image = UIImageJPEGRepresentation(image, 0.01);
                                            } failure:^(NSURLRequest *request, NSHTTPURLResponse *response, NSError *error) {
                                                cell.profilePicture.image = [UIImage imageNamed:@"avatar_contact-card"];

                                            }];
    }else {
        [cell.profilePicture setImage:[UIImage imageNamed:@"avatar_contact-card"]];
    }
} else {
    [cell.profilePicture setImage:[UIImage imageNamed:@"avatar_contact-card"]];
    [cell configurateCellWithContactDetails:(ContactDetails *)contact];
}
return cell;

如何解决此问题并为正确的单元格显示适当的图像?

【问题讨论】:

    标签: ios objective-c uitableview afnetworking


    【解决方案1】:

    您可能没有正确准备表格单元格以供重复使用。当图像请求由于重用或网络请求失败而无法到达其 UIImageView 接收器时,这可能会导致您看到旧图像。此外,URL 请求可能会出现乱序,导致正确的图像被之前可能已延迟的网络请求覆盖。

    您需要确保清除profilePicture imageView 并取消任何可能导致图像稍后更改的待处理 URL 请求。

    将以下代码放在您的 UITableViewCell 子类中:

    - (void)prepareForReuse {
        [self cancelImageRequestOperation];
        self.profilePicture.image = nil;
    }
    

    确保你已经在你的 UITableViewCell 中导入了 UIImageView+AFNetworking 类别:

    #import UIImageView+AFNetworking.h
    

    【讨论】:

      【解决方案2】:

      在单元格出列后,尝试在tableView:cellForRowAtIndexPath: 的开头将cell.profilePicture.image 设置为nil

      - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
          Cell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell"];
          cell.profilePicture.image = nil;    
      
          //the rest of your code.
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2016-02-05
        • 2011-10-29
        • 2015-07-28
        • 2011-12-06
        • 1970-01-01
        • 1970-01-01
        • 2011-08-30
        • 2017-01-07
        相关资源
        最近更新 更多