3

我正在尝试将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 (image) 吗?我只需要将“图像”分配给属性“缩略图”,我该怎么做?

4

1 回答 1

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

}
于 2013-06-13T08:25:18.633 回答