Skip to content

Instantly share code, notes, and snippets.

@bang590
Created October 26, 2015 13:22
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bang590/edfbcd69580d7d2b6a34 to your computer and use it in GitHub Desktop.
Save bang590/edfbcd69580d7d2b6a34 to your computer and use it in GitHub Desktop.
Change SDWebImage cache directory
@implementation SDImageCache
...
+ (void)changeDefaultCacheDiskToDocument
{
Method origMethod = class_getInstanceMethod(self, @selector(initWithNamespace:));
Method replaceMethod = class_getInstanceMethod(self, @selector(initWithNamespaceInDocument:));
if (origMethod && replaceMethod) {
method_exchangeImplementations(origMethod, replaceMethod);
}
}
- (id)initWithNamespaceInDocument:(NSString *)ns {
if ((self = [super init])) {
NSString *fullNamespace = [@"image." stringByAppendingString:ns];
// Create IO serial queue
_ioQueue = dispatch_queue_create("com.hackemist.SDWebImageCache", DISPATCH_QUEUE_SERIAL);
// Init default values
_maxCacheAge = kDefaultCacheMaxCacheAge;
// Init the memory cache
_memCache = [[NSCache alloc] init];
_memCache.name = fullNamespace;
// Init the disk cache
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
_diskCachePath = [paths[0] stringByAppendingPathComponent:fullNamespace];
dispatch_sync(_ioQueue, ^{
_fileManager = [NSFileManager new];
});
#if TARGET_OS_IPHONE
// Subscribe to app events
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(clearMemory) name:UIApplicationDidReceiveMemoryWarningNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(cleanDisk) name:UIApplicationWillTerminateNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(backgroundCleanDisk) name:UIApplicationDidEnterBackgroundNotification object:nil];
#endif
}
return self;
}
...
@end
/***********Usage:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
[SDImageCache changeDefaultCacheDiskToDocument];
...
}
*****************/
@dongchong
Copy link

你好,麻烦问下你这个更改默认cachePath的方法,类名称和SDImageCache一样,那如何调用来将原来的方法给覆盖掉呢?
还是说你直接改的SDImageCache类呢?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment