gpt4 book ai didi

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

转载 作者:塔克拉玛干 更新时间:2023-11-02 10:00:26 26 4
gpt4 key购买 nike

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

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

enter image description here

- (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 上设置图像。如果单击“调试 View 层次结构”

enter image description here

xcode 运行时底部工具栏上的按钮 你看到了什么。是两个不同 ImageView 上的图像。我不认为一个 UIImageView 可以共享两个 UIImages

关于ios - 单个 UIImageView 显示两个单独的图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31519407/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com