gpt4 book ai didi

ios - 下载缩略图后重新加载 UItableViewCell

转载 作者:塔克拉玛干 更新时间:2023-11-02 07:46:01 25 4
gpt4 key购买 nike

我有一个应用程序,在其中的 tableViewCell 中下载图像并将其设置为缩略图。我的问题是,我可以看到缩略图刷新的唯一方法是单击另一个选项卡,然后返回到 tableViewCell 所在的选项卡。下面是设置缩略图的代码。

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *cellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier forIndexPath:indexPath];
PFObject *message = [self.messages objectAtIndex:indexPath.row];
cell.textLabel.text = [message objectForKey:@"username"];
cell.imageView.image = [UIImage imageNamed:@"Treehouse.png"];
PFFile *thumbnail = nil;

if (thumbnailArray.count > 0){
thumbnail = [[thumbnailArray objectAtIndex:indexPath.row] objectForKey:@"imageFile"];
dispatch_async(backgroundQueue, ^{

NSURL *thumbnailURL = [[NSURL alloc]initWithString:thumbnail.url];
NSData *thumbnailData = [NSData dataWithContentsOfURL:thumbnailURL];

[[cell imageView]setImage:[UIImage imageWithData:thumbnailData]];
[tableView reloadRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationNone];


});
}


return cell;
}

最佳答案

问题是.,

  1. 单元格已加载。
  2. 图像下载被触发。
  3. 下载图像后,将重新加载单元格。
  4. 图像再次下载。
  5. 没有图像缓存或加载图像的逻辑

声明一个 NSMutableDictionary @property (strong) NSMutableDictionary *imagesDictionary; 并实例化它 self.imagesDictionary = [NSMutableDictionary dictionary];

在索引路径行的单元格中,在下载之前检查缓存是否存在。

PFFile *thumbnail = nil;

if (thumbnailArray.count > 0)
{
//If image is present in cache load from cache
UIImage *image = [self.imagesDictionary objectForKey:indexPath];
if (image)
{
[cell.imageView setImage:image];
}
else
{
//If Image is not present fire a download
thumbnail = [[thumbnailArray objectAtIndex:indexPath.row] objectForKey:@"imageFile"];
dispatch_async(backgroundQueue, ^{

NSURL *thumbnailURL = [[NSURL alloc]initWithString:thumbnail.url];
NSData *thumbnailData = [NSData dataWithContentsOfURL:thumbnailURL];

//Cahce it to your image dictionary
[self.imagesDictionary setObject:[UIImage imageWithData:thumbnailData] forKey:indexPath];
//Reload the dictionary
[tableView reloadRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationNone];


});


}

关于ios - 下载缩略图后重新加载 UItableViewCell,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24171914/

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