Make Better Things



I like to make better things.

image caching in iphone sdk

If you’re pulling in web data such as images that doesn’t change but needs to be viewed multiple times, you’ll want to cache it. Caching images dramatically improves the performance of scrolling tables and other views.

Here is a very simple class, with which you can implement caching of images in your iPhone app.

ImgCache.h :


#import <UIKit/UIKit.h>

@interface ImgCache : NSObject {

}

- (void) cacheImage: (NSString *) ImageURLString;
- (UIImage *) getImage: (NSString *) ImageURLString;

@end

ImgCache.m :


#import "ImgCache.h"
#define TMP NSTemporaryDirectory()

@implementation ImgCache

- (void) cacheImage: (NSString *) ImageURLString
{
 NSURL *ImageURL = [NSURL URLWithString: ImageURLString];
 //generating unique name for the cached file with ImageURLString so you can retrive it back
 NSMutableString *tmpStr = [NSMutableString stringWithString:ImageURLString];
 [tmpStr replaceOccurrencesOfString:@"/" withString:@"-" options:1 range:NSMakeRange(0, [tmpStr length])];

 NSString *filename = [NSString stringWithFormat:@"%@",tmpStr];

 NSString *uniquePath = [TMP stringByAppendingPathComponent: filename];

 // Check for file existence
 if(![[NSFileManager defaultManager] fileExistsAtPath: uniquePath])
 {
 // The file doesn't exist, we should get a copy of it

 // Fetch image
 NSData *data = [[NSData alloc] initWithContentsOfURL: ImageURL];
 UIImage *image = [[UIImage alloc] initWithData: data];

 // Is it PNG or JPG/JPEG?
 // Running the image representation function writes the data from the image to a file
 if([ImageURLString rangeOfString: @".png" options: NSCaseInsensitiveSearch].location != NSNotFound)
 {
 [UIImagePNGRepresentation(image) writeToFile: uniquePath atomically: YES];
 }
 else if(
 [ImageURLString rangeOfString: @".jpg" options: NSCaseInsensitiveSearch].location != NSNotFound ||
 [ImageURLString rangeOfString: @".jpeg" options: NSCaseInsensitiveSearch].location != NSNotFound
 )
 {
 [UIImageJPEGRepresentation(image, 100) writeToFile: uniquePath atomically: YES];
 }
 }
}



- (UIImage *) getImage: (NSString *) ImageURLString
{
 NSMutableString *tmpStr = [NSMutableString stringWithString:ImageURLString];
 [tmpStr replaceOccurrencesOfString:@"/" withString:@"-" options:1 range:NSMakeRange(0, [tmpStr length])];

 NSString *filename = [NSString stringWithFormat:@"%@",tmpStr];
 NSString *uniquePath = [TMP stringByAppendingPathComponent: filename];

 UIImage *image;

 // Check for a cached version
 if([[NSFileManager defaultManager] fileExistsAtPath: uniquePath])
 {
 NSData *data = [[NSData alloc] initWithContentsOfFile:uniquePath];
 image = [[UIImage alloc] initWithData:data] ; // this is the cached image
 }
 else
 {
 // get a new one
 [self cacheImage: ImageURLString];
 NSData *data = [[NSData alloc] initWithContentsOfURL:uniquePath];
 image = [[UIImage alloc] initWithData:data];

 }

 return image;
}


@end

Category: iPhone

Tagged:

2 Responses

  1. shankar says:

    can you please send xcode code in code memory leak problems

    sorry for bad english.

    waiting for your response

    thanks
    shankar gidwani

  2. db says:

    Thanks for this, modified it a bit to suit my needs by making the cacheimage: to return a UIImage instead.

Leave a Reply