diff --git a/OGImage/OGImageCache.h b/OGImage/OGImageCache.h index dc27172..c73b0d4 100644 --- a/OGImage/OGImageCache.h +++ b/OGImage/OGImageCache.h @@ -19,6 +19,8 @@ typedef void (^OGImageCacheCompletionBlock)(__OGImage *image); + (NSString *)filePathForKey:(NSString *)key; ++ (NSURL *)fileURLForKey:(NSString *)key; + /** * Check in-memory and on-disk caches for image corresponding to `key`. `block` * called on main queue when check is complete. If `image` parameter is `nil`, @@ -46,10 +48,6 @@ typedef void (^OGImageCacheCompletionBlock)(__OGImage *image); */ - (void)purgeMemoryCacheForKey:(NSString *)key andWait:(BOOL)wait; -/** - * - */ - /** * Remove cached images from disk that haven't been accessed since `date` * If `wait` is `YES` this will block the calling thread until the purge diff --git a/OGImage/OGImageCache.m b/OGImage/OGImageCache.m index 7a6bd5c..10f82ab 100644 --- a/OGImage/OGImageCache.m +++ b/OGImage/OGImageCache.m @@ -12,18 +12,6 @@ static OGImageCache *OGImageCacheShared; -NSString *OGImageCachePath() { - // generate the cache path: /Library/Application Support//OGImageCache, - // creating the directories as needed - NSArray *array = NSSearchPathForDirectoriesInDomains(NSApplicationSupportDirectory, NSUserDomainMask, YES); - if (nil == array || 0 == [array count]) { - return nil; - } - NSString *cachePath = [[array[0] stringByAppendingPathComponent:[[NSBundle mainBundle] bundleIdentifier]] stringByAppendingPathComponent:@"OGImageCache"]; - [[NSFileManager defaultManager] createDirectoryAtPath:cachePath withIntermediateDirectories:YES attributes:nil error:nil]; - return cachePath; -} - NSURL *OGImageCacheURL() { // generate the cache path: /Library/Application Support//OGImageCache, // creating the directories as needed @@ -62,7 +50,11 @@ + (NSString *)MD5:(NSString *)string { } + (NSString *)filePathForKey:(NSString *)key { - return [OGImageCachePath() stringByAppendingPathComponent:[OGImageCache MD5:key]]; + return [[OGImageCache fileURLForKey:key] path]; +} + ++ (NSURL *)fileURLForKey:(NSString *)key { + return [OGImageCacheURL() URLByAppendingPathComponent:[OGImageCache MD5:key]]; } - (id)init { @@ -97,8 +89,8 @@ - (void)imageForKey:(NSString *)key block:(OGImageCacheCompletionBlock)block { dispatch_suspend(_cacheFileTasksQueue); dispatch_async(_cacheFileReadQueue, ^{ // Check to see if the image is cached locally - NSString *cachePath = [OGImageCache filePathForKey:(key)]; - __OGImage *image = [[__OGImage alloc] initWithDataAtURL:[NSURL fileURLWithPath:cachePath]]; + NSURL *cacheURL = [OGImageCache fileURLForKey:(key)]; + __OGImage *image = [[__OGImage alloc] initWithDataAtURL:cacheURL]; // if we have the image in the on-disk cache, store it to the in-memory cache if (nil != image) { [_memoryCache setObject:image forKey:key]; @@ -116,7 +108,7 @@ - (void)setImage:(__OGImage *)image forKey:(NSString *)key { NSParameterAssert(nil != key); [_memoryCache setObject:image forKey:key]; dispatch_async(_cacheFileTasksQueue, ^{ - NSURL *fileURL = [NSURL fileURLWithPath:[OGImageCache filePathForKey:key]]; + NSURL *fileURL = [OGImageCache fileURLForKey:key]; NSError *error; if(![image writeToURL:fileURL error:&error]) { NSLog(@"[OGImageCache ERROR] failed to write image with error %@ %s %d", error, __FILE__, __LINE__); @@ -131,9 +123,8 @@ - (void)setImage:(__OGImage *)image forKey:(NSString *)key { - (void)purgeCache:(BOOL)wait { [_memoryCache removeAllObjects]; void (^purgeFilesBlock)(void) = ^{ - NSString *cachePath = OGImageCachePath(); - for (NSString *file in [[NSFileManager defaultManager] enumeratorAtPath:cachePath]) { - [[NSFileManager defaultManager] removeItemAtPath:[cachePath stringByAppendingPathComponent:file] error:nil]; + for (NSURL *url in [[NSFileManager defaultManager] enumeratorAtURL:OGImageCacheURL() includingPropertiesForKeys:nil options:0 errorHandler:nil]) { + [[NSFileManager defaultManager] removeItemAtURL:url error:nil]; } }; if (YES == wait) { @@ -148,10 +139,10 @@ - (void)purgeCacheForKey:(NSString *)key andWait:(BOOL)wait { [self purgeMemoryCacheForKey:key andWait:wait]; - NSString *cachedFilePath = [[self class] filePathForKey:key]; + NSURL *cachedFileURL = [[self class] fileURLForKey:key]; void (^purgeFileBlock)(void) =^{ - [[NSFileManager defaultManager] removeItemAtPath:cachedFilePath error:nil]; + [[NSFileManager defaultManager] removeItemAtURL:cachedFileURL error:nil]; }; if (YES == wait) {