Skip to content

Instantly share code, notes, and snippets.

@tsafrir
Last active May 19, 2016 15:47
Show Gist options
  • Save tsafrir/5627179 to your computer and use it in GitHub Desktop.
Save tsafrir/5627179 to your computer and use it in GitHub Desktop.
Use SDWebImage to download an image the display in UITableViewCell with fade in and the image cache build in SDWebImage. https://github.com/rs/SDWebImage
#import "UIImageView+WebCache.h"
// load the image in:
// - (void)configureCell:(MyCell*)cell atIndexPath:(NSIndexPath*)indexPath
// or in - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
NSString *imageURLString = item.imageURL;
if (imageURLString) {
NSURL *url = [NSURL URLWithString:imageURLString];
if (url) {
UIImage *placeholder = [UIImage imageNamed:@"image-shader"];
__weak UIImageView *imageView = cell.largeImageView;
[cell.largeImageView setImageWithURL:url placeholderImage:placeholder options:SDWebImageRetryFailed
progress:nil completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType) {
if (!error && image) {
imageView.image = image;
if (cacheType != SDImageCacheTypeMemory) { // fade in only on first download
imageView.alpha = 0.0;
[UIView animateWithDuration:0.30 animations:^{
imageView.alpha = 1.0;
}];
}
else {
imageView.alpha = 1.0;
}
}
}];
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment