缩略图下载后重新加载UItableViewCell

时间:2014-06-11 20:22:58

标签: ios objective-c uitableview

我有一个应用程序,其中在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;
}

3 个答案:

答案 0 :(得分:2)

问题是。,

  1. 正在加载单元格。
  2. 触发图像下载。
  3. 下载图像后,将重新加载单元格。
  4. 再次下载图片。
  5. 没有用于加载图像的图像或逻辑的缓存
  6. 声明NSMutableDictionary @property (strong) NSMutableDictionary *imagesDictionary;并将其实例化self.imagesDictionary = [NSMutableDictionary dictionary];

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

    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];
    
    
        });
    
    
    }
    

答案 1 :(得分:1)

我建议您使用AFNetworkings类别UIImage + AFNetworking,它允许您只管理[imageView setImageWithURL:(NSURL)]来管理后台的所有内容。您不必担心加载图像,缓存图像或类似的东西。它会为你缓存它。

https://github.com/AFNetworking/AFNetworking

答案 2 :(得分:1)

以下代码有助于解决我的问题

- (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];
           dispatch_sync(dispatch_get_main_queue(), ^{
               [[cell imageView]setImage:[UIImage imageWithData:thumbnailData]];
               [tableView reloadInputViews];
           });
   });
   }


    return cell;
}

问题是重新加载TableView是在一个单独的线程上调用的。一旦我将图像的设置移动到主线程并从那里调用reloadInputViews,我的问题就解决了。