@@ -25,6 +25,7 @@ - (id)init
25
25
memCache = [[NSMutableDictionary alloc ] init ];
26
26
27
27
// Init the disk cache
28
+ storeDataQueue = [[NSMutableDictionary alloc ] init ];
28
29
NSArray *paths = NSSearchPathForDirectoriesInDomains (NSCachesDirectory, NSUserDomainMask, YES );
29
30
diskCachePath = [[[paths objectAtIndex: 0 ] stringByAppendingPathComponent: @" ImageCache" ] retain ];
30
31
@@ -72,6 +73,7 @@ - (void)dealloc
72
73
[memCache release ], memCache = nil ;
73
74
[diskCachePath release ], diskCachePath = nil ;
74
75
[cacheInQueue release ], cacheInQueue = nil ;
76
+ [storeDataQueue release ], storeDataQueue = nil ;
75
77
76
78
[[NSNotificationCenter defaultCenter ] removeObserver: self ];
77
79
@@ -105,25 +107,43 @@ - (NSString *)cachePathForKey:(NSString *)key
105
107
106
108
- (void )storeKeyToDisk : (NSString *)key
107
109
{
108
- UIImage *image = [[self imageFromKey: key fromDisk: YES ] retain ]; // be thread safe with no lock
110
+ // Can't use defaultManager another thread
111
+ NSFileManager *fileManager = [[NSFileManager alloc ] init ];
109
112
110
- if (image != nil )
113
+ NSData *data = [storeDataQueue objectForKey: key];
114
+ if (data)
111
115
{
112
- [[NSFileManager defaultManager ] createFileAtPath: [self cachePathForKey: key] contents: UIImageJPEGRepresentation (image, (CGFloat)1.0 ) attributes: nil ];
113
- [image release ];
116
+ [fileManager createFileAtPath: [self cachePathForKey: key] contents: data attributes: nil ];
117
+ @synchronized (storeDataQueue)
118
+ {
119
+ [storeDataQueue removeObjectForKey: key];
120
+ }
121
+ }
122
+ else
123
+ {
124
+ // If no data representation given, convert the UIImage in JPEG and store it
125
+ // This trick is more CPU/memory intensive and doesn't preserve alpha channel
126
+ UIImage *image = [[self imageFromKey: key fromDisk: YES ] retain ]; // be thread safe with no lock
127
+ if (image)
128
+ {
129
+ [fileManager createFileAtPath: [self cachePathForKey: key] contents: UIImageJPEGRepresentation (image, (CGFloat)1.0 ) attributes: nil ];
130
+ [image release ];
131
+ }
114
132
}
133
+
134
+ [fileManager release ];
115
135
}
116
136
117
137
#pragma mark ImageCache
118
138
119
- - (void )storeImage : (UIImage *)image forKey : (NSString *)key
139
+ - (void )storeImage : (UIImage *)image imageData : ( NSData *) data forKey : (NSString *)key toDisk : ( BOOL ) toDisk
120
140
{
121
- [self storeImage: image forKey: key toDisk: YES ];
122
- }
141
+ if (!image || !key)
142
+ {
143
+ return ;
144
+ }
123
145
124
- - (void )storeImage : (UIImage *)image forKey : (NSString *)key toDisk : (BOOL )toDisk
125
- {
126
- if (image == nil || key == nil )
146
+ if (toDisk && !data)
127
147
{
128
148
return ;
129
149
}
@@ -132,10 +152,23 @@ - (void)storeImage:(UIImage *)image forKey:(NSString *)key toDisk:(BOOL)toDisk
132
152
133
153
if (toDisk)
134
154
{
155
+ [storeDataQueue setObject: data forKey: key];
135
156
[cacheInQueue addOperation: [[[NSInvocationOperation alloc ] initWithTarget: self selector: @selector (storeKeyToDisk: ) object: key] autorelease ]];
157
+
136
158
}
137
159
}
138
160
161
+ - (void )storeImage : (UIImage *)image forKey : (NSString *)key
162
+ {
163
+ [self storeImage: image imageData: nil forKey: key toDisk: YES ];
164
+ }
165
+
166
+ - (void )storeImage : (UIImage *)image forKey : (NSString *)key toDisk : (BOOL )toDisk
167
+ {
168
+ [self storeImage: image imageData: nil forKey: key toDisk: toDisk];
169
+ }
170
+
171
+
139
172
- (UIImage *)imageFromKey : (NSString *)key
140
173
{
141
174
return [self imageFromKey: key fromDisk: YES ];
@@ -152,7 +185,7 @@ - (UIImage *)imageFromKey:(NSString *)key fromDisk:(BOOL)fromDisk
152
185
153
186
if (!image && fromDisk)
154
187
{
155
- image = [[UIImage alloc ] initWithData: [ NSData dataWithContentsOfFile: [ self cachePathForKey: key] ]];
188
+ image = [[UIImage alloc ] initWithContentsOfFile: [ self cachePathForKey: key]];
156
189
if (image != nil )
157
190
{
158
191
[memCache setObject: image forKey: key];
0 commit comments