SDWebImage与自定义UITableView单元格图像

时间:2013-06-13 03:19:41

标签: ios objective-c uiimageview uiimage sdwebimage

我正在尝试将SDWebImage库与自定义UITableViewCell一起使用。以下是从Web服务下载图像的方法:

- (void) downloadThumbnails:(NSURL *)finalUrl
{
    SDWebImageManager *manager = [SDWebImageManager sharedManager];
    [manager downloadWithURL:finalUrl
                     options:0
                    progress:nil
                   completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, BOOL finished)
     {
         if (image)
         {
             // do something with image
             // THERE I HAVE TO ASSIGN the property "thumbnail" with the "image"
             // So it can be used by the tableview controller class
         }
     }];

}

上面的代码位于名为RSSItem的单独文件中。虽然我的UITableViewController课程有以下cellForRowAtIndexPath方法:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"ItemsCell";

    ItemsViewCell *cell = (ItemsViewCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];

    RSSItem *item = [[channel items] objectAtIndex:[indexPath row]];

    cell.titleLabel.text = [item title];
    cell.creatorLabel.text = [item creator];
    cell.pubTimeLabel.text = [item pubTime];
    cell.thumbContainer.image = [item thumbnail];

    return cell;
}

有人可以指出我如何在downloadThumbnails方法中配置if(图像)?我只需要将“图像”分配给属性“缩略图”,我该怎么做?

1 个答案:

答案 0 :(得分:4)

看起来您已经使用正确的方法使用SDWebImage异步下载图像。您需要做的就是将缩略图属性设置为“图像”。以下是如何做到这一点:

- (void) downloadThumbnails:(NSURL *)finalUrl
{
    SDWebImageManager *manager = [SDWebImageManager sharedManager];
    [manager downloadWithURL:finalUrl
                     options:0
                    progress:nil
                   completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, BOOL finished)
     {
         if (image)
         {
             [self setThumbnail:image];
         }
     }];

}