Skip to content

Instantly share code, notes, and snippets.

@natelyman
Created January 9, 2012 10:16
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 natelyman/1582321 to your computer and use it in GitHub Desktop.
Save natelyman/1582321 to your computer and use it in GitHub Desktop.
Cached UITableViewCell that loads images from a remote source.
//
// NLCachedImageTableViewCell.h
// niners
//
// Created by Lyman, Nathan(nlyman) on 1/9/12.
// Copyright (c) 2012 __MyCompanyName__. All rights reserved.
//
#import <UIKit/UIKit.h>
@interface NLCachedImageTableViewCell : UITableViewCell
@property (strong,nonatomic) NSString *imageUrlString;
@property (strong) UIImage *placeholderImage;
@end
//
// NLCachedImageTableViewCell.m
// Nate Lyman
//
// Created by Lyman, Nathan(nlyman) on 1/9/12.
// Copyright (c) 2012 NateLyman.com. All rights reserved.
//
#import "NLCachedImageTableViewCell.h"
#import <CommonCrypto/CommonDigest.h>
@interface NLCachedImageTableViewCell()
- (NSString*) sha1: (NSString *)str;
@end
@implementation NLCachedImageTableViewCell
@synthesize imageUrlString = _imageUrlString;
@synthesize placeholderImage = _placeholderImage;
- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
if (self) {
// Initialization code
}
return self;
}
- (void)setSelected:(BOOL)selected animated:(BOOL)animated
{
[super setSelected:selected animated:animated];
// Configure the view for the selected state
}
- (void) setImageUrlString:(NSString *)imageUrlString
{
_imageUrlString = imageUrlString;
NSString *key = [self sha1:imageUrlString];
NSString* documentsPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
NSString *fileToSave = [documentsPath stringByAppendingPathComponent:[NSString stringWithFormat:@"%@.jpg",key]];
BOOL fileExists = [[NSFileManager defaultManager] fileExistsAtPath:fileToSave];
if (!fileExists)
{
dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0ul);
dispatch_async(queue, ^{
NSURL *imageURL=[NSURL URLWithString:imageUrlString];
NSData *imageData=[NSData dataWithContentsOfURL:imageURL];
UIImage *image = [[UIImage alloc] initWithData:imageData];
NSData *jpegData = UIImageJPEGRepresentation(image,100);
[[NSFileManager defaultManager] createFileAtPath:fileToSave contents:jpegData attributes:nil];
dispatch_sync(dispatch_get_main_queue(), ^{
self.imageView.image = image;
[self setNeedsLayout];
});
});
self.imageView.image = self.placeholderImage;
}else{
UIImage *image = [UIImage imageWithContentsOfFile:fileToSave];
if(!image) image = self.placeholderImage;
self.imageView.image = image;
}
}
-(NSString*) sha1: (NSString *)str
{
const char *cstr = [str cStringUsingEncoding:NSUTF8StringEncoding];
NSData *data = [NSData dataWithBytes:cstr length:str.length];
uint8_t digest[CC_SHA1_DIGEST_LENGTH];
CC_SHA1(data.bytes, data.length, digest);
NSMutableString* output = [NSMutableString stringWithCapacity:CC_SHA1_DIGEST_LENGTH * 2];
for(int i = 0; i < CC_SHA1_DIGEST_LENGTH; i++)
[output appendFormat:@"%02x", digest[i]];
return output;
}
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment