I have a class that stores a UIImage in an NSMutableDictionary. If UIImage already exists in the dictionary, I simply return this object, and not create a new UIImage.
#import "Cache.h"
@implementation Cache
static NSMutableDictionary *dict;
+ (UIImage*)loadImageFromWeb:(NSString*)imageName{
if (!dict) dict = [[NSMutableDictionary dictionary] retain];
UIImage* image = [dict objectForKey:imageName];
if (!image)
{
image = [UIImage imageWithData: [NSData dataWithContentsOfURL: [NSURL URLWithString: imageName]]];
if (image)
{
[dict setObject:image forKey:imageName];
}
}
return image;
}
source
share