DMImageCache.m 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. /*
  2. * This file is part of the DMWebImage package.
  3. * (c) Dailymotion - Olivier Poitrey <rs@dailymotion.com>
  4. *
  5. * For the full copyright and license information, please view the LICENSE
  6. * file that was distributed with this source code.
  7. */
  8. #import "DMImageCache.h"
  9. #import <CommonCrypto/CommonDigest.h>
  10. static NSInteger kMaxCacheAge = 60*60*24*7; // 1 week
  11. static DMImageCache *instance;
  12. @implementation DMImageCache
  13. #pragma mark NSObject
  14. - (id)init
  15. {
  16. if (self = [super init])
  17. {
  18. cache = [[NSMutableDictionary alloc] init];
  19. [[NSNotificationCenter defaultCenter] addObserver:self
  20. selector:@selector(didReceiveMemoryWarning:)
  21. name:UIApplicationDidReceiveMemoryWarningNotification
  22. object:nil];
  23. [[NSNotificationCenter defaultCenter] addObserver:self
  24. selector:@selector(willTerminate)
  25. name:UIApplicationWillTerminateNotification
  26. object:nil];
  27. // Init the cache
  28. NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
  29. diskCachePath = [[[paths objectAtIndex:0] stringByAppendingPathComponent:@"ImageCache"] retain];
  30. if (![[NSFileManager defaultManager] fileExistsAtPath:diskCachePath])
  31. {
  32. [[NSFileManager defaultManager] createDirectoryAtPath:diskCachePath attributes:nil];
  33. }
  34. }
  35. return self;
  36. }
  37. - (void)dealloc
  38. {
  39. [cache release];
  40. [[NSNotificationCenter defaultCenter] removeObserver:self
  41. name:UIApplicationDidReceiveMemoryWarningNotification
  42. object:nil];
  43. [[NSNotificationCenter defaultCenter] removeObserver:self
  44. name:UIApplicationWillTerminateNotification
  45. object:nil];
  46. [super dealloc];
  47. }
  48. - (void)didReceiveMemoryWarning:(void *)object
  49. {
  50. [self clearMemory];
  51. }
  52. - (void)willTerminate
  53. {
  54. [self cleanDisk];
  55. }
  56. #pragma mark ImageCache (private)
  57. - (NSString *)cachePathForKey:(NSString *)key
  58. {
  59. const char *str = [key UTF8String];
  60. unsigned char r[CC_MD5_DIGEST_LENGTH];
  61. CC_MD5(str, strlen(str), r);
  62. NSString *filename = [NSString stringWithFormat:@"%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x",
  63. r[0], r[1], r[2], r[3], r[4], r[5], r[6], r[7], r[8], r[9], r[10], r[11], r[12], r[13], r[14], r[15]];
  64. return [diskCachePath stringByAppendingPathComponent:filename];
  65. }
  66. #pragma mark ImageCache
  67. + (DMImageCache *)sharedImageCache
  68. {
  69. if (instance == nil)
  70. {
  71. instance = [[DMImageCache alloc] init];
  72. }
  73. return instance;
  74. }
  75. - (void)storeImage:(UIImage *)image forKey:(NSString *)key
  76. {
  77. [self storeImage:image forKey:key toDisk:YES];
  78. }
  79. - (void)storeImage:(UIImage *)image forKey:(NSString *)key toDisk:(BOOL)toDisk
  80. {
  81. if (image == nil)
  82. {
  83. return;
  84. }
  85. [cache setObject:image forKey:key];
  86. if (toDisk)
  87. {
  88. [[NSFileManager defaultManager] createFileAtPath:[self cachePathForKey:key] contents:UIImageJPEGRepresentation(image, 1.0) attributes:nil];
  89. }
  90. }
  91. - (UIImage *)imageFromKey:(NSString *)key
  92. {
  93. return [self imageFromKey:key fromDisk:YES];
  94. }
  95. - (UIImage *)imageFromKey:(NSString *)key fromDisk:(BOOL)fromDisk
  96. {
  97. UIImage *image = [cache objectForKey:key];
  98. if (!image && fromDisk)
  99. {
  100. image = [[UIImage alloc] initWithData:[NSData dataWithContentsOfFile:[self cachePathForKey:key]]];
  101. if (image != nil)
  102. {
  103. [cache setObject:image forKey:key];
  104. [image release];
  105. }
  106. }
  107. return image;
  108. }
  109. - (void)removeImageForKey:(NSString *)key
  110. {
  111. [cache removeObjectForKey:key];
  112. [[NSFileManager defaultManager] removeItemAtPath:[self cachePathForKey:key] error:nil];
  113. }
  114. - (void)clearMemory
  115. {
  116. [cache removeAllObjects];
  117. }
  118. - (void)clearDisk
  119. {
  120. [[NSFileManager defaultManager] removeItemAtPath:diskCachePath error:nil];
  121. [[NSFileManager defaultManager] createDirectoryAtPath:diskCachePath attributes:nil];
  122. }
  123. - (void)cleanDisk
  124. {
  125. NSDate *expirationDate = [NSDate dateWithTimeIntervalSinceNow:-kMaxCacheAge];
  126. NSDirectoryEnumerator *fileEnumerator = [[NSFileManager defaultManager] enumeratorAtPath:diskCachePath];
  127. for (NSString *fileName in fileEnumerator)
  128. {
  129. NSString *filePath = [diskCachePath stringByAppendingPathComponent:fileName];
  130. NSDictionary *attrs = [[NSFileManager defaultManager] attributesOfItemAtPath:filePath error:nil];
  131. if ([[[attrs fileModificationDate] laterDate:expirationDate] isEqualToDate:expirationDate])
  132. {
  133. [[NSFileManager defaultManager] removeItemAtPath:filePath error:nil];
  134. }
  135. }
  136. }
  137. @end