Merge pull request #1453 from Mutix/master
Added API to save image NSData to disk cache.
Showing
4 changed files
with
77 additions
and
22 deletions
@@ -135,6 +135,14 @@ typedef void(^SDWebImageCalculateSizeBlock)(NSUInteger fileCount, NSUInteger tot | @@ -135,6 +135,14 @@ typedef void(^SDWebImageCalculateSizeBlock)(NSUInteger fileCount, NSUInteger tot | ||
135 | - (void)storeImage:(UIImage *)image recalculateFromImage:(BOOL)recalculate imageData:(NSData *)imageData forKey:(NSString *)key toDisk:(BOOL)toDisk; | 135 | - (void)storeImage:(UIImage *)image recalculateFromImage:(BOOL)recalculate imageData:(NSData *)imageData forKey:(NSString *)key toDisk:(BOOL)toDisk; |
136 | 136 | ||
137 | /** | 137 | /** |
138 | + * Store image NSData into disk cache at the given key. | ||
139 | + * | ||
140 | + * @param imageData The image data to store | ||
141 | + * @param key The unique image cache key, usually it's image absolute URL | ||
142 | + */ | ||
143 | +- (void)storeImageDataToDisk:(NSData *)imageData forKey:(NSString *)key; | ||
144 | + | ||
145 | +/** | ||
138 | * Query the disk cache asynchronously. | 146 | * Query the disk cache asynchronously. |
139 | * | 147 | * |
140 | * @param key The unique key used to store the wanted image | 148 | * @param key The unique key used to store the wanted image |
@@ -241,23 +241,7 @@ FOUNDATION_STATIC_INLINE NSUInteger SDCacheCostForImage(UIImage *image) { | @@ -241,23 +241,7 @@ FOUNDATION_STATIC_INLINE NSUInteger SDCacheCostForImage(UIImage *image) { | ||
241 | #endif | 241 | #endif |
242 | } | 242 | } |
243 | 243 | ||
244 | - if (data) { | ||
245 | - if (![_fileManager fileExistsAtPath:_diskCachePath]) { | ||
246 | - [_fileManager createDirectoryAtPath:_diskCachePath withIntermediateDirectories:YES attributes:nil error:NULL]; | ||
247 | - } | ||
248 | - | ||
249 | - // get cache Path for image key | ||
250 | - NSString *cachePathForKey = [self defaultCachePathForKey:key]; | ||
251 | - // transform to NSUrl | ||
252 | - NSURL *fileURL = [NSURL fileURLWithPath:cachePathForKey]; | ||
253 | - | ||
254 | - [_fileManager createFileAtPath:cachePathForKey contents:data attributes:nil]; | ||
255 | - | ||
256 | - // disable iCloud backup | ||
257 | - if (self.shouldDisableiCloud) { | ||
258 | - [fileURL setResourceValue:[NSNumber numberWithBool:YES] forKey:NSURLIsExcludedFromBackupKey error:nil]; | ||
259 | - } | ||
260 | - } | 244 | + [self storeImageDataToDisk:data forKey:key]; |
261 | }); | 245 | }); |
262 | } | 246 | } |
263 | } | 247 | } |
@@ -270,6 +254,29 @@ FOUNDATION_STATIC_INLINE NSUInteger SDCacheCostForImage(UIImage *image) { | @@ -270,6 +254,29 @@ FOUNDATION_STATIC_INLINE NSUInteger SDCacheCostForImage(UIImage *image) { | ||
270 | [self storeImage:image recalculateFromImage:YES imageData:nil forKey:key toDisk:toDisk]; | 254 | [self storeImage:image recalculateFromImage:YES imageData:nil forKey:key toDisk:toDisk]; |
271 | } | 255 | } |
272 | 256 | ||
257 | +- (void)storeImageDataToDisk:(NSData *)imageData forKey:(NSString *)key { | ||
258 | + | ||
259 | + if (!imageData) { | ||
260 | + return; | ||
261 | + } | ||
262 | + | ||
263 | + if (![_fileManager fileExistsAtPath:_diskCachePath]) { | ||
264 | + [_fileManager createDirectoryAtPath:_diskCachePath withIntermediateDirectories:YES attributes:nil error:NULL]; | ||
265 | + } | ||
266 | + | ||
267 | + // get cache Path for image key | ||
268 | + NSString *cachePathForKey = [self defaultCachePathForKey:key]; | ||
269 | + // transform to NSUrl | ||
270 | + NSURL *fileURL = [NSURL fileURLWithPath:cachePathForKey]; | ||
271 | + | ||
272 | + [_fileManager createFileAtPath:cachePathForKey contents:imageData attributes:nil]; | ||
273 | + | ||
274 | + // disable iCloud backup | ||
275 | + if (self.shouldDisableiCloud) { | ||
276 | + [fileURL setResourceValue:[NSNumber numberWithBool:YES] forKey:NSURLIsExcludedFromBackupKey error:nil]; | ||
277 | + } | ||
278 | +} | ||
279 | + | ||
273 | - (BOOL)diskImageExistsWithKey:(NSString *)key { | 280 | - (BOOL)diskImageExistsWithKey:(NSString *)key { |
274 | BOOL exists = NO; | 281 | BOOL exists = NO; |
275 | 282 |
@@ -122,6 +122,7 @@ | @@ -122,6 +122,7 @@ | ||
122 | DA248D50195472AA00390AB0 /* Frameworks */, | 122 | DA248D50195472AA00390AB0 /* Frameworks */, |
123 | DA248D51195472AA00390AB0 /* Resources */, | 123 | DA248D51195472AA00390AB0 /* Resources */, |
124 | D6347736BDF64FC5A4D078A4 /* Copy Pods Resources */, | 124 | D6347736BDF64FC5A4D078A4 /* Copy Pods Resources */, |
125 | + 4B51E412BA3594400947AC71 /* Embed Pods Frameworks */, | ||
125 | ); | 126 | ); |
126 | buildRules = ( | 127 | buildRules = ( |
127 | ); | 128 | ); |
@@ -170,6 +171,21 @@ | @@ -170,6 +171,21 @@ | ||
170 | /* End PBXResourcesBuildPhase section */ | 171 | /* End PBXResourcesBuildPhase section */ |
171 | 172 | ||
172 | /* Begin PBXShellScriptBuildPhase section */ | 173 | /* Begin PBXShellScriptBuildPhase section */ |
174 | + 4B51E412BA3594400947AC71 /* Embed Pods Frameworks */ = { | ||
175 | + isa = PBXShellScriptBuildPhase; | ||
176 | + buildActionMask = 2147483647; | ||
177 | + files = ( | ||
178 | + ); | ||
179 | + inputPaths = ( | ||
180 | + ); | ||
181 | + name = "Embed Pods Frameworks"; | ||
182 | + outputPaths = ( | ||
183 | + ); | ||
184 | + runOnlyForDeploymentPostprocessing = 0; | ||
185 | + shellPath = /bin/sh; | ||
186 | + shellScript = "\"${SRCROOT}/Pods/Target Support Files/Pods-ios/Pods-ios-frameworks.sh\"\n"; | ||
187 | + showEnvVarsInLog = 0; | ||
188 | + }; | ||
173 | D6347736BDF64FC5A4D078A4 /* Copy Pods Resources */ = { | 189 | D6347736BDF64FC5A4D078A4 /* Copy Pods Resources */ = { |
174 | isa = PBXShellScriptBuildPhase; | 190 | isa = PBXShellScriptBuildPhase; |
175 | buildActionMask = 2147483647; | 191 | buildActionMask = 2147483647; |
@@ -14,7 +14,7 @@ | @@ -14,7 +14,7 @@ | ||
14 | 14 | ||
15 | #import "SDImageCache.h" | 15 | #import "SDImageCache.h" |
16 | 16 | ||
17 | -NSString *kImageTestKey = @"TestImageKey"; | 17 | +NSString *kImageTestKey = @"TestImageKey.jpg"; |
18 | 18 | ||
19 | @interface SDImageCacheTests : XCTestCase | 19 | @interface SDImageCacheTests : XCTestCase |
20 | @property (strong, nonatomic) SDImageCache *sharedImageCache; | 20 | @property (strong, nonatomic) SDImageCache *sharedImageCache; |
@@ -185,6 +185,25 @@ NSString *kImageTestKey = @"TestImageKey"; | @@ -185,6 +185,25 @@ NSString *kImageTestKey = @"TestImageKey"; | ||
185 | expect(path).notTo.beNil; | 185 | expect(path).notTo.beNil; |
186 | } | 186 | } |
187 | 187 | ||
188 | +// TODO -- Testing image data insertion | ||
189 | + | ||
190 | +- (void)testInsertionOfImageData { | ||
191 | + | ||
192 | + NSData *imageData = [NSData dataWithContentsOfFile:[self testImagePath]]; | ||
193 | + [self.sharedImageCache storeImageDataToDisk:imageData forKey:kImageTestKey]; | ||
194 | + | ||
195 | + UIImage *storedImageFromMemory = [self.sharedImageCache imageFromMemoryCacheForKey:kImageTestKey]; | ||
196 | + expect(storedImageFromMemory).to.equal(nil); | ||
197 | + | ||
198 | + NSString *cachePath = [self.sharedImageCache defaultCachePathForKey:kImageTestKey]; | ||
199 | + NSData *storedImageData = [NSData dataWithContentsOfFile:cachePath]; | ||
200 | + expect([storedImageData isEqualToData:imageData]).will.beTruthy; | ||
201 | + | ||
202 | + [self.sharedImageCache diskImageExistsWithKey:kImageTestKey completion:^(BOOL isInCache) { | ||
203 | + expect(isInCache).to.equal(YES); | ||
204 | + }]; | ||
205 | +} | ||
206 | + | ||
188 | #pragma mark Helper methods | 207 | #pragma mark Helper methods |
189 | 208 | ||
190 | - (void)clearAllCaches{ | 209 | - (void)clearAllCaches{ |
@@ -193,9 +212,14 @@ NSString *kImageTestKey = @"TestImageKey"; | @@ -193,9 +212,14 @@ NSString *kImageTestKey = @"TestImageKey"; | ||
193 | } | 212 | } |
194 | 213 | ||
195 | - (UIImage *)imageForTesting{ | 214 | - (UIImage *)imageForTesting{ |
196 | - NSBundle *testBundle=[NSBundle bundleForClass:[self class]]; | ||
197 | - NSString *testBundlePath=[testBundle pathForResource:@"TestImage" ofType:@"jpg"]; | ||
198 | - return [UIImage imageWithContentsOfFile:testBundlePath]; | 215 | + |
216 | + return [UIImage imageWithContentsOfFile:[self testImagePath]]; | ||
217 | +} | ||
218 | + | ||
219 | +- (NSString *)testImagePath { | ||
220 | + | ||
221 | + NSBundle *testBundle = [NSBundle bundleForClass:[self class]]; | ||
222 | + return [testBundle pathForResource:@"TestImage" ofType:@"jpg"]; | ||
199 | } | 223 | } |
200 | 224 | ||
201 | -@end | ||
225 | +@end |
-
Please register or login to post a comment