...
|
...
|
@@ -25,6 +25,7 @@ static SDImageCache *instance; |
|
|
memCache = [[NSMutableDictionary alloc] init];
|
|
|
|
|
|
// Init the disk cache
|
|
|
storeDataQueue = [[NSMutableDictionary alloc] init];
|
|
|
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
|
|
|
diskCachePath = [[[paths objectAtIndex:0] stringByAppendingPathComponent:@"ImageCache"] retain];
|
|
|
|
...
|
...
|
@@ -72,6 +73,7 @@ static SDImageCache *instance; |
|
|
[memCache release], memCache = nil;
|
|
|
[diskCachePath release], diskCachePath = nil;
|
|
|
[cacheInQueue release], cacheInQueue = nil;
|
|
|
[storeDataQueue release], storeDataQueue = nil;
|
|
|
|
|
|
[[NSNotificationCenter defaultCenter] removeObserver:self];
|
|
|
|
...
|
...
|
@@ -105,25 +107,43 @@ static SDImageCache *instance; |
|
|
|
|
|
- (void)storeKeyToDisk:(NSString *)key
|
|
|
{
|
|
|
UIImage *image = [[self imageFromKey:key fromDisk:YES] retain]; // be thread safe with no lock
|
|
|
// Can't use defaultManager another thread
|
|
|
NSFileManager *fileManager = [[NSFileManager alloc] init];
|
|
|
|
|
|
if (image != nil)
|
|
|
NSData *data = [storeDataQueue objectForKey:key];
|
|
|
if (data)
|
|
|
{
|
|
|
[[NSFileManager defaultManager] createFileAtPath:[self cachePathForKey:key] contents:UIImageJPEGRepresentation(image, (CGFloat)1.0) attributes:nil];
|
|
|
[image release];
|
|
|
[fileManager createFileAtPath:[self cachePathForKey:key] contents:data attributes:nil];
|
|
|
@synchronized(storeDataQueue)
|
|
|
{
|
|
|
[storeDataQueue removeObjectForKey:key];
|
|
|
}
|
|
|
}
|
|
|
else
|
|
|
{
|
|
|
// If no data representation given, convert the UIImage in JPEG and store it
|
|
|
// This trick is more CPU/memory intensive and doesn't preserve alpha channel
|
|
|
UIImage *image = [[self imageFromKey:key fromDisk:YES] retain]; // be thread safe with no lock
|
|
|
if (image)
|
|
|
{
|
|
|
[fileManager createFileAtPath:[self cachePathForKey:key] contents:UIImageJPEGRepresentation(image, (CGFloat)1.0) attributes:nil];
|
|
|
[image release];
|
|
|
}
|
|
|
}
|
|
|
|
|
|
[fileManager release];
|
|
|
}
|
|
|
|
|
|
#pragma mark ImageCache
|
|
|
|
|
|
- (void)storeImage:(UIImage *)image forKey:(NSString *)key
|
|
|
- (void)storeImage:(UIImage *)image imageData:(NSData *)data forKey:(NSString *)key toDisk:(BOOL)toDisk
|
|
|
{
|
|
|
[self storeImage:image forKey:key toDisk:YES];
|
|
|
}
|
|
|
if (!image || !key)
|
|
|
{
|
|
|
return;
|
|
|
}
|
|
|
|
|
|
- (void)storeImage:(UIImage *)image forKey:(NSString *)key toDisk:(BOOL)toDisk
|
|
|
{
|
|
|
if (image == nil || key == nil)
|
|
|
if (toDisk && !data)
|
|
|
{
|
|
|
return;
|
|
|
}
|
...
|
...
|
@@ -132,10 +152,23 @@ static SDImageCache *instance; |
|
|
|
|
|
if (toDisk)
|
|
|
{
|
|
|
[storeDataQueue setObject:data forKey:key];
|
|
|
[cacheInQueue addOperation:[[[NSInvocationOperation alloc] initWithTarget:self selector:@selector(storeKeyToDisk:) object:key] autorelease]];
|
|
|
|
|
|
}
|
|
|
}
|
|
|
|
|
|
- (void)storeImage:(UIImage *)image forKey:(NSString *)key
|
|
|
{
|
|
|
[self storeImage:image imageData:nil forKey:key toDisk:YES];
|
|
|
}
|
|
|
|
|
|
- (void)storeImage:(UIImage *)image forKey:(NSString *)key toDisk:(BOOL)toDisk
|
|
|
{
|
|
|
[self storeImage:image imageData:nil forKey:key toDisk:toDisk];
|
|
|
}
|
|
|
|
|
|
|
|
|
- (UIImage *)imageFromKey:(NSString *)key
|
|
|
{
|
|
|
return [self imageFromKey:key fromDisk:YES];
|
...
|
...
|
@@ -152,7 +185,7 @@ static SDImageCache *instance; |
|
|
|
|
|
if (!image && fromDisk)
|
|
|
{
|
|
|
image = [[UIImage alloc] initWithData:[NSData dataWithContentsOfFile:[self cachePathForKey:key]]];
|
|
|
image = [[UIImage alloc] initWithContentsOfFile:[self cachePathForKey:key]];
|
|
|
if (image != nil)
|
|
|
{
|
|
|
[memCache setObject:image forKey:key];
|
...
|
...
|
|