Search code examples
objective-ciosmknetworkkit

MKNetworkKit - displaying cached images


UPDATE AT BOTTOM

I branched off of the MKNetworkKit Flickr demo for this. I have several images on a webserver I want to display in a table. I have a UITableViewCell subclass, ImageCell.

Here is the custom method for retrieving remote images:

-(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;

I have inspected the contents of my default cache directory, and there are now items inside. Scrolling the table constantly now prints "Image is in cache". The problem is, the cell's imageView never updates. Mugunth has a class method automaticallyNotifiesObserversForKey: but I don't ever see it implemented anywhere. I'm guessing that there's another step involved to tell the tableView instance to update that row with the new contents.

Thanks for your input.

UPDATE

I got this to work by using a custom Interface Builder xib file with a Cell and a UIImageView IBOutlet. Not sure why it wasn't working with self.imageView.image, and would be interested to find out why, exactly. I still consider this an open question because I'd like to just use the standard UITableViewCell class.


Solution

  • I the call to prepareForReuse. Are you setting the self.imageView.image to nil? If yes, dont do it because it looks like it's never re-initialized.