Skip to content

Instantly share code, notes, and snippets.

@MugunthKumar
Created January 8, 2012 00:11
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save MugunthKumar/1576556 to your computer and use it in GitHub Desktop.
Save MugunthKumar/1576556 to your computer and use it in GitHub Desktop.
MKNetworkEngine and UIImageView
[AppDelegate.imageFetcher imageAtURL:self.profile.avatarURL
onCompletion:^(UIImage *image, NSURL *url, BOOL isInCache)
{
// this "if" is to prevent images flashing repeatedly when cells are reused.
if ([[self.profile.avatarURL absoluteString] isEqualToString:[url absoluteString]]) {
self.avatarImageView.image = resizedImage;
}
}];
@Burnsoft
Copy link

Burnsoft commented Jan 8, 2012

thanks for the quick reply Mugunth.
I'm testing this with a twitter stream and using this:

if ([[[tweet objectForKey:@"user"] objectForKey:@"profile_image_url"] isEqualToString:[fetchedURL absoluteString]]) { cell.imageView.image = fetchedImage; }

how should I best maintain a reference to the originally requested URL when scrolling and the subsequent delay in the request?
the newly loadd images still 'cycle'

many thanks once again
Nik

@MugunthKumar
Copy link
Author

MugunthKumar commented Jan 8, 2012 via email

@Burnsoft
Copy link

Burnsoft commented Jan 8, 2012

Hi Mugunth, apologies if I'm unclear. Basically the tableview image is not refreshed until the tableview reloads.

    [ApplicationDelegate.engine imageAtURL:[NSURL URLWithString:[[tweet objectForKey:@"user"] objectForKey:@"profile_image_url"]] onCompletion:^(UIImage *fetchedImage, NSURL *fetchedURL, BOOL isInCache) {
        if(isInCache){
            cell.imageView.image = fetchedImage;

        }else if ([[[tweet objectForKey:@"user"] objectForKey:@"profile_image_url"] isEqualToString:[fetchedURL absoluteString]]) {
            cell.imageView.image = fetchedImage;
        }else{
            cell.imageView.image = nil;


        }
    }];

There is likely a very simple solution which I'm missing, but any assistance in understanding how I can check the MKNetworkKit cache before starting the block request and thereby having the image show immediately would be greatly appreciated.

Previously I would maintain a dictionary of cached images in my app Delegate and either show if the URL was already available in the cache or start an NSOperation to grab it.

cheers
Nik

@MugunthKumar
Copy link
Author

@Burnsoft
Copy link

thanks Mugunth, I intend to. I saw on twitter you had written this. much appreciated.
Regards
Nik

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment