MKNetworkKit - 显示缓存的图像

时间:2012-08-12 19:23:37

标签: objective-c ios mknetworkkit

在底部更新

我为此分享了MKNetworkKit Flickr演示。我想在一个表格中显示的网络服务器上有几个图像。我有一个UITableViewCell子类,ImageCell。

以下是检索远程图像的自定义方法:

-(void)setImageForCell:(NSString *)remoteFileName {

    self.loadingImageURLString =
    [NSString stringWithFormat:@"http://myserver.com/%@.png",remoteFileName];
    self.imageLoadingOperation = [ApplicationDelegate.imageDownloader imageAtURL:[NSURL URLWithString:self.loadingImageURLString]
                            onCompletion:^(UIImage *fetchedImage, NSURL *url, BOOL isInCache) {

                                if([self.loadingImageURLString isEqualToString:[url absoluteString]]) {

                                    if (isInCache) {
                                        self.imageView.image = fetchedImage;
                                        [self.contentView drawRect:self.contentView.frame];
                                        NSLog(@"Image is in cache");
                                    } else {
                                        self.imageView.image = fetchedImage;
                                        [self.contentView drawRect:self.contentView.frame];
                                        NSLog(@"Image is not in cache");
                                    }
                                }
                            }];   
}
    //TableView.h
    //it is called like this
   //in cellForRowAtIndexPath...
   ImageCell *cell = (ImageCell *)[tableView dequeue...etc];
 MyObject *obj = [dataSource objectAtIndex:indexPath.row];
 cell.textLabel.text = obj.name;
 [cell setImageForCell:obj.name];
 return cell;

我检查了默认缓存目录的内容,现在里面有项目。不断滚动表现在打印“图像在缓存中”。问题是,单元格的imageView永远不会更新。 Mugunth有一个类方法自动通知ObserversForKey:但我从来没有看到它在任何地方实现过。我猜测还有另一个步骤要告诉tableView实例使用新内容更新该行。

感谢您的意见。

更新

我通过使用带有Cell和UIImageView IBOutlet的自定义Interface Builder xib文件来实现此目的。不知道为什么它不能与self.imageView.image一起使用,并且有兴趣找到原因。我仍然认为这是一个悬而未决的问题,因为我想使用标准的UITableViewCell类。

2 个答案:

答案 0 :(得分:3)

我打电话给prepareForReuse。你是否将self.imageView.image设置为nil? 如果是的话,不要这样做,因为它看起来好像从未重新初始化。

答案 1 :(得分:1)

为什么从代码中调用“drawRect”。这是亵渎神明! 检查您的imageView并检查IB连接是否良好。

相关问题