-
Notifications
You must be signed in to change notification settings - Fork 0
/
CSAbstractImageCacheDomain.m
108 lines (89 loc) · 3.19 KB
/
CSAbstractImageCacheDomain.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
//
// AbstractImageCacheDomain.m
// Calineczka
//
// Created by ciukes on 25/10/2009.
// Copyright 2009 __MyCompanyName__. All rights reserved.
//
#import "CSAbstractImageCacheDomain.h"
#define CACHE_FOLDER_NAME @"Cache"
@implementation CSAbstractImageCacheDomain
@synthesize _fileManager;
-(id)init {
self._fileManager=[NSFileManager defaultManager];
return self;
}
-(NSString*)diskFolder {
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *cacheDirectoryName = [documentsDirectory stringByAppendingPathComponent:@"ImageCacheFolder_General"];
return cacheDirectoryName;
}
-(NSUInteger)lifetime {
return IMAGE_FILE_LIFETIME;
}
-(NSUInteger)memoryCacheSize {
return MEMORY_CACHE_SIZE;
}
-(void)removeAllImages {
NSAutoreleasePool *pool=[[NSAutoreleasePool alloc] init];
@try{
NSString *cacheDirectoryName = [self diskFolder];
NSError *error = nil;
NSString *suffix=@"deleteme";
NSString *dstPath=[[NSString alloc] initWithFormat:@"%@-%@",cacheDirectoryName,suffix];
NSFileManager *fileManager=[NSFileManager defaultManager];
[fileManager moveItemAtPath:cacheDirectoryName toPath:dstPath error:&error];
[error release]; error=nil;
[fileManager createDirectoryAtPath:cacheDirectoryName withIntermediateDirectories:YES attributes:nil error:&error];
[error release]; error=nil;
// ignore the "move"command result
// important is if the directory exists; it could be created before - don't care.
if([fileManager isDeletableFileAtPath:dstPath]){
[fileManager removeItemAtPath:dstPath error:&error];
DebugLog(@"removed: [%@] error:[%@]",dstPath,error);
[error release];error=nil;
}
[dstPath release];
}@catch (NSException *exception) {
NSLog(@"Caught [%@:%@]",exception.name, exception.reason);
}
@try{
// you should use drain instead of release.
[pool drain];
}@catch (NSException *exception) {
NSLog(@"Caught [%@:%@]",exception.name, exception.reason);
}
}
-(BOOL)imageExists:(NSString*)key {
NSString *fileName = [self getFileNameForKey:key];
return [_fileManager fileExistsAtPath:fileName];
}
-(NSData*)getImageContents:(NSString*)key {
NSString *fileName = [self getFileNameForKey:key];
NSData *result=[NSData dataWithContentsOfFile:fileName];
return result;
}
-(void)addImage:(UIImage *)image withKey:(NSString *)key {
NSString *fileName = [self getFileNameForKey:key];
[UIImagePNGRepresentation(image) writeToFile:fileName atomically:YES];
}
- (NSString *)getCacheDirectoryName {
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *cacheDirectoryName = [documentsDirectory stringByAppendingPathComponent:[self cacheFolderName]];
return cacheDirectoryName;
}
-(NSString*)cacheFolderName {
return CACHE_FOLDER_NAME;
}
- (NSString *)getFileNameForKey:(NSString *)key {
NSString *cacheDirectoryName = [self getCacheDirectoryName];
NSString *fileName = [cacheDirectoryName stringByAppendingPathComponent:key];
return fileName;
}
-(void)dealloc {
[_fileManager release];
[super dealloc];
}
@end