单个 UIImageView 显示两个单独的图像

Single UIImageView is displaying two separate images

我的应用程序中有一个 UITableView,它为每个单元格从互联网上提取图像。在拉取该图像时,我在那里有一个占位符图像(在 InterfaceBuilder 中设置)。但是,当我将 UIImageView 设置为来自网络的新图像时,它只是覆盖了旧图像而不是替换它。这很好,除了您实际上可以看到旧图像,因为两张图像都是 aspect-fit 并且不一定占据整个 imageView

是不是线程问题?有没有一种方法可以设置占位符图像,使其立即出现,甚至在 cellForRowAtIndexPath?

之前

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    // Get a new or recycled cell
    RosterListingCellTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"RosterCell" forIndexPath:indexPath];

    LineListing *thisRosterListing = [self.rosters objectAtIndex:indexPath.row];
    cell.playerNumberLabel.text = [NSString stringWithFormat:@"#%@",thisRosterListing.number];
    cell.playerNameLabel.text = thisRosterListing.name;


    cell.imageView.backgroundColor = [UIColor clearColor];

    cell.imageView.contentMode = UIViewContentModeScaleAspectFill;
    cell.imageView.clipsToBounds = YES;

    UIImage *playerImage = [self.imageCache objectForKey:thisRosterListing.playerImageURL];
    cell.imageView.image = playerImage;
    if (playerImage == nil) {

        NSURLSessionConfiguration *sessionConfig =
        [NSURLSessionConfiguration defaultSessionConfiguration];

        NSURLSession *session = [NSURLSession sessionWithConfiguration:sessionConfig delegate:self delegateQueue:nil];
        thisRosterListing.playerImageURL = [thisRosterListing.playerImageURL stringByReplacingOccurrencesOfString:@"small" withString:@"medium"];
        NSURLSessionDataTask *imageData = [session dataTaskWithURL:[NSURL URLWithString: thisRosterListing.playerImageURL]
                                                completionHandler:^(NSData *data,
                                                                    NSURLResponse *response,
                                                                    NSError *error) {
                                                    // handle NSData
                                                    UIImage *image = [UIImage imageWithData:data];
                                                    thisRosterListing.image = image;
                                                    [self.imageCache setObject:image forKey:thisRosterListing.playerImageURL];


                                                    dispatch_async(dispatch_get_main_queue(), ^{
                                                        cell.imageView.image = image;
                                                        [self.tableView reloadData];
                                                    });
                                                }];
        [imageData resume];
    }


    UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 20, 20)];
    imageView.image = [UIImage imageNamed:@"indicator"];
    cell.accessoryView = imageView;
    cell.backgroundColor = [UIColor clearColor];

    // set selection color
    UIView *myBackView = [[UIView alloc] initWithFrame:cell.frame];
    myBackView.backgroundColor = [UIColor clearColor];
    cell.selectedBackgroundView = myBackView;

    return cell;
}

我认为您是在两个不同的 UIImageView 上设置图像。如果单击 "Debug view hierachy"

当 xcode 为 运行 时底部工具栏上的

按钮 你看到了什么。是两个不同图像视图上的图像。我不认为一个 UIImageView 可以共享两个 UIImages