2014-05-19 32 views
1

我正在构建一个iOS应用程序,它将XML项目添加到TableView(当然还有其他东西)。如果文章的缩略图字段为空,我想在TableView单元格或默认占位符中显示文章的缩略图。从远程URL添加图像后,TableView滚动口吃

下面的代码添加了文章中的缩略图,但之后导致滚动的结果相当多。如果我只为每个单元格使用占位符图像,一切都很好。我猜测我可能没有使用最理想的方法来添加缩略图,不知道是否会导致问题。

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { 
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"idCellNewsTitle"]; 
    if (cell == nil) { 
     cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:@"idCellNewsTitle"]; 
    } 
    NSDictionary *dict = [self.arrNewsData objectAtIndex:indexPath.row]; 
    cell.imageView.image = [UIImage imageNamed:@"holder-small.jpg"]; 
    cell.textLabel.text = [dict objectForKey:@"title"]; 
    cell.detailTextLabel.text = [dict objectForKey:@"pubDate"]; 
    if ([dict objectForKey:@"thumbnail"] != nil) { 
     NSData *imgData = [NSData dataWithContentsOfURL:[NSURL URLWithString: [dict objectForKey:@"thumbnail"]]]; 
     if (imgData) { 
      UIImage *image = [UIImage imageWithData:imgData]; 
      if (image) { 
       cell.imageView.image = image; 
      } 
     } 
    } 
    return cell; 
} 

编码在最新的XCode,测试在模拟器和一个新的iPod - 结果是既相同。在应用程序运行期间没有任何警告或错误,滚动时CPU的峰值为1%,内存保持在16 MB左右。

更新 - 视频 这里是一个视频展示了这个问题的任何未来的菜鸟与比较 - 第一个例子是一个占位符,第二个是没有。 Video on YouTube

+0

撷取图片不这样做 “dataWithContentsOfURL” 在主线程上,使用一个后台线程完成这个任务 – Pawan

+0

我刚学的iOS编程 - 你可以在此上阐述一下或者用一些示例代码发布答案?我真的很感激它。感谢您的反馈:) – NightMICU

+1

您还应该缓存图像,以便在每次UITableViewCell变为可见时不会下载相同的图像。 – Jonathan

回答

2

取代你的代码中的cellForRowAtIndexPath:

此行if ([dict objectForKey:@"thumbnail"] != nil)

这样,你在后台加载每个图像,一旦其装载相应的单元格更新的mainThread后。

dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_BACKGROUND, 0), ^(void) { 
     NSData *imgData = NSData *imgData = [NSData dataWithContentsOfURL:[NSURL URLWithString: [dict objectForKey:@"thumbnail"]]]; 
     if (imgData) { 
      UIImage *image = [UIImage imageWithData:imgData]; 

      dispatch_sync(dispatch_get_main_queue(), ^(void) { 
      UIImage *image = [UIImage imageWithData:imgData]; 
         if (image) { 
          cell.imageView.image = image; 
      } 
     }); 
    }); 

LazyTableImages Reference

+0

好的,清理了一下代码后,我有这个工作 - 除了一件事外,它非常甜蜜。滚动现在是zippy,但当我滚动时,我看到我的占位符闪烁进出。我是否应该省略占位符图像,并且只有在不存在缩略图的情况下才将'cell.imageView'省略掉? – NightMICU

+0

我知道,你会抱怨这个。此问题的解决方案是缓存图像。我会建议你使用这些解决方案的任何一个。 [SDWebImage](https://github.com/rs/SDWebImage)“我总是喜欢这个”[AsyncImageView](https://github.com/nicklockwood/AsyncImageView) – Pawan

+0

通灵!谢谢你的提示。 +1和为你选择的答案:) – NightMICU

0

使用GCD

// Fetch using GCD 
    dispatch_queue_t downloadThumbnailQueue = dispatch_queue_create("Get Photo Thumbnail", NULL); 
    dispatch_async(downloadThumbnailQueue, ^{ 
    UIImage *image = [self getTopPlacePhotoThumbnail:photo]; 
    dispatch_async(dispatch_get_main_queue(), ^{ 
     UITableViewCell *cellToUpdate = [self.tableView cellForRowAtIndexPath:indexPath]; // create a copy of the cell to avoid keeping a strong pointer to "cell" since that one may have been reused by the time the block is ready to update it. 
     if (cellToUpdate != nil) { 
      [cellToUpdate.imageView setImage:image]; 
      [cellToUpdate setNeedsLayout]; 
     } 
    }); 
    }); 
+0

这会后'if([dict objectForKey:@“thumbnail”]!= nil){'?' – NightMICU