SDImageCache.m 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553
  1. // This file is based on third party code, see below for the original author
  2. // and original license.
  3. // Modifications are (c) by Threema GmbH and licensed under the AGPLv3.
  4. /*
  5. * This file is part of the SDWebImage package.
  6. * (c) Olivier Poitrey <rs@dailymotion.com>
  7. *
  8. * For the full copyright and license information, please view the LICENSE
  9. * file that was distributed with this source code.
  10. */
  11. #import "SDImageCache.h"
  12. #import "SDWebImageDecoder.h"
  13. #import "UIImage+MultiFormat.h"
  14. #import <CommonCrypto/CommonDigest.h>
  15. static const NSInteger kDefaultCacheMaxCacheAge = 60 * 60 * 24 * 7; // 1 week
  16. // PNG signature bytes and data (below)
  17. static unsigned char kPNGSignatureBytes[8] = {0x89, 0x50, 0x4E, 0x47, 0x0D, 0x0A, 0x1A, 0x0A};
  18. static NSData *kPNGSignatureData = nil;
  19. BOOL ImageDataHasPNGPreffix(NSData *data);
  20. BOOL ImageDataHasPNGPreffix(NSData *data) {
  21. NSUInteger pngSignatureLength = [kPNGSignatureData length];
  22. if ([data length] >= pngSignatureLength) {
  23. if ([[data subdataWithRange:NSMakeRange(0, pngSignatureLength)] isEqualToData:kPNGSignatureData]) {
  24. return YES;
  25. }
  26. }
  27. return NO;
  28. }
  29. @interface SDImageCache ()
  30. @property (strong, nonatomic) NSCache *memCache;
  31. @property (strong, nonatomic) NSString *diskCachePath;
  32. @property (strong, nonatomic) NSMutableArray *customPaths;
  33. @property (SDDispatchQueueSetterSementics, nonatomic) dispatch_queue_t ioQueue;
  34. @end
  35. @implementation SDImageCache {
  36. NSFileManager *_fileManager;
  37. }
  38. + (SDImageCache *)sharedImageCache {
  39. static dispatch_once_t once;
  40. static id instance;
  41. dispatch_once(&once, ^{
  42. instance = [self new];
  43. });
  44. return instance;
  45. }
  46. - (id)init {
  47. return [self initWithNamespace:@"default"];
  48. }
  49. - (id)initWithNamespace:(NSString *)ns {
  50. if ((self = [super init])) {
  51. NSString *fullNamespace = [@"com.hackemist.SDWebImageCache." stringByAppendingString:ns];
  52. // initialise PNG signature data
  53. kPNGSignatureData = [NSData dataWithBytes:kPNGSignatureBytes length:8];
  54. // Create IO serial queue
  55. _ioQueue = dispatch_queue_create("com.hackemist.SDWebImageCache", DISPATCH_QUEUE_SERIAL);
  56. // Init default values
  57. _maxCacheAge = kDefaultCacheMaxCacheAge;
  58. // Init the memory cache
  59. _memCache = [[NSCache alloc] init];
  60. _memCache.name = fullNamespace;
  61. // Init the disk cache
  62. NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
  63. _diskCachePath = [paths[0] stringByAppendingPathComponent:fullNamespace];
  64. // Set decompression to YES
  65. _shouldDecompressImages = YES;
  66. dispatch_sync(_ioQueue, ^{
  67. _fileManager = [NSFileManager new];
  68. });
  69. #if TARGET_OS_IPHONE
  70. // Subscribe to app events
  71. [[NSNotificationCenter defaultCenter] addObserver:self
  72. selector:@selector(clearMemory)
  73. name:UIApplicationDidReceiveMemoryWarningNotification
  74. object:nil];
  75. [[NSNotificationCenter defaultCenter] addObserver:self
  76. selector:@selector(cleanDisk)
  77. name:UIApplicationWillTerminateNotification
  78. object:nil];
  79. [[NSNotificationCenter defaultCenter] addObserver:self
  80. selector:@selector(backgroundCleanDisk)
  81. name:UIApplicationDidEnterBackgroundNotification
  82. object:nil];
  83. #endif
  84. }
  85. return self;
  86. }
  87. - (void)dealloc {
  88. [[NSNotificationCenter defaultCenter] removeObserver:self];
  89. SDDispatchQueueRelease(_ioQueue);
  90. }
  91. - (void)addReadOnlyCachePath:(NSString *)path {
  92. if (!self.customPaths) {
  93. self.customPaths = [NSMutableArray new];
  94. }
  95. if (![self.customPaths containsObject:path]) {
  96. [self.customPaths addObject:path];
  97. }
  98. }
  99. - (NSString *)cachePathForKey:(NSString *)key inPath:(NSString *)path {
  100. NSString *filename = [self cachedFileNameForKey:key];
  101. return [path stringByAppendingPathComponent:filename];
  102. }
  103. - (NSString *)defaultCachePathForKey:(NSString *)key {
  104. return [self cachePathForKey:key inPath:self.diskCachePath];
  105. }
  106. #pragma mark SDImageCache (private)
  107. - (NSString *)cachedFileNameForKey:(NSString *)key {
  108. const char *str = [key UTF8String];
  109. if (str == NULL) {
  110. str = "";
  111. }
  112. unsigned char r[CC_MD5_DIGEST_LENGTH];
  113. CC_MD5(str, (CC_LONG)strlen(str), r);
  114. NSString *filename = [NSString stringWithFormat:@"%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x",
  115. 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]];
  116. return filename;
  117. }
  118. #pragma mark ImageCache
  119. - (void)storeImage:(UIImage *)image recalculateFromImage:(BOOL)recalculate imageData:(NSData *)imageData forKey:(NSString *)key toDisk:(BOOL)toDisk {
  120. if (!image || !key) {
  121. return;
  122. }
  123. [self.memCache setObject:image forKey:key cost:image.size.height * image.size.width * image.scale * image.scale];
  124. if (toDisk) {
  125. dispatch_async(self.ioQueue, ^{
  126. NSData *data = imageData;
  127. if (image && (recalculate || !data)) {
  128. #if TARGET_OS_IPHONE
  129. // We need to determine if the image is a PNG or a JPEG
  130. // PNGs are easier to detect because they have a unique signature (http://www.w3.org/TR/PNG-Structure.html)
  131. // The first eight bytes of a PNG file always contain the following (decimal) values:
  132. // 137 80 78 71 13 10 26 10
  133. // We assume the image is PNG, in case the imageData is nil (i.e. if trying to save a UIImage directly),
  134. // we will consider it PNG to avoid loosing the transparency
  135. BOOL imageIsPng = YES;
  136. // But if we have an image data, we will look at the preffix
  137. if ([imageData length] >= [kPNGSignatureData length]) {
  138. imageIsPng = ImageDataHasPNGPreffix(imageData);
  139. }
  140. if (imageIsPng) {
  141. data = UIImagePNGRepresentation(image);
  142. }
  143. else {
  144. data = UIImageJPEGRepresentation(image, (CGFloat)1.0);
  145. }
  146. #else
  147. data = [NSBitmapImageRep representationOfImageRepsInArray:image.representations usingType: NSJPEGFileType properties:nil];
  148. #endif
  149. }
  150. if (data) {
  151. if (![_fileManager fileExistsAtPath:_diskCachePath]) {
  152. [_fileManager createDirectoryAtPath:_diskCachePath withIntermediateDirectories:YES attributes:nil error:NULL];
  153. }
  154. [_fileManager createFileAtPath:[self defaultCachePathForKey:key] contents:data attributes:nil];
  155. }
  156. });
  157. }
  158. }
  159. - (void)storeImage:(UIImage *)image forKey:(NSString *)key {
  160. [self storeImage:image recalculateFromImage:YES imageData:nil forKey:key toDisk:YES];
  161. }
  162. - (void)storeImage:(UIImage *)image forKey:(NSString *)key toDisk:(BOOL)toDisk {
  163. [self storeImage:image recalculateFromImage:YES imageData:nil forKey:key toDisk:toDisk];
  164. }
  165. - (BOOL)diskImageExistsWithKey:(NSString *)key {
  166. BOOL exists = NO;
  167. // this is an exception to access the filemanager on another queue than ioQueue, but we are using the shared instance
  168. // from apple docs on NSFileManager: The methods of the shared NSFileManager object can be called from multiple threads safely.
  169. exists = [[NSFileManager defaultManager] fileExistsAtPath:[self defaultCachePathForKey:key]];
  170. return exists;
  171. }
  172. - (void)diskImageExistsWithKey:(NSString *)key completion:(SDWebImageCheckCacheCompletionBlock)completionBlock {
  173. dispatch_async(_ioQueue, ^{
  174. BOOL exists = [_fileManager fileExistsAtPath:[self defaultCachePathForKey:key]];
  175. if (completionBlock) {
  176. dispatch_async(dispatch_get_main_queue(), ^{
  177. completionBlock(exists);
  178. });
  179. }
  180. });
  181. }
  182. - (UIImage *)imageFromMemoryCacheForKey:(NSString *)key {
  183. return [self.memCache objectForKey:key];
  184. }
  185. - (UIImage *)imageFromDiskCacheForKey:(NSString *)key {
  186. // First check the in-memory cache...
  187. UIImage *image = [self imageFromMemoryCacheForKey:key];
  188. if (image) {
  189. return image;
  190. }
  191. // Second check the disk cache...
  192. UIImage *diskImage = [self diskImageForKey:key];
  193. if (diskImage) {
  194. CGFloat cost = diskImage.size.height * diskImage.size.width * diskImage.scale * diskImage.scale;
  195. [self.memCache setObject:diskImage forKey:key cost:cost];
  196. }
  197. return diskImage;
  198. }
  199. - (NSData *)diskImageDataBySearchingAllPathsForKey:(NSString *)key {
  200. NSString *defaultPath = [self defaultCachePathForKey:key];
  201. NSData *data = [NSData dataWithContentsOfFile:defaultPath];
  202. if (data) {
  203. return data;
  204. }
  205. for (NSString *path in self.customPaths) {
  206. NSString *filePath = [self cachePathForKey:key inPath:path];
  207. NSData *imageData = [NSData dataWithContentsOfFile:filePath];
  208. if (imageData) {
  209. return imageData;
  210. }
  211. }
  212. return nil;
  213. }
  214. - (UIImage *)diskImageForKey:(NSString *)key {
  215. NSData *data = [self diskImageDataBySearchingAllPathsForKey:key];
  216. if (data) {
  217. UIImage *image = [UIImage sd_imageWithData:data];
  218. image = [self scaledImageForKey:key image:image];
  219. if (self.shouldDecompressImages) {
  220. image = [UIImage decodedImageWithImage:image];
  221. }
  222. return image;
  223. }
  224. else {
  225. return nil;
  226. }
  227. }
  228. - (UIImage *)scaledImageForKey:(NSString *)key image:(UIImage *)image {
  229. return SDScaledImageForKey(key, image);
  230. }
  231. - (NSOperation *)queryDiskCacheForKey:(NSString *)key done:(SDWebImageQueryCompletedBlock)doneBlock {
  232. if (!doneBlock) {
  233. return nil;
  234. }
  235. if (!key) {
  236. doneBlock(nil, SDImageCacheTypeNone);
  237. return nil;
  238. }
  239. // First check the in-memory cache...
  240. UIImage *image = [self imageFromMemoryCacheForKey:key];
  241. if (image) {
  242. doneBlock(image, SDImageCacheTypeMemory);
  243. return nil;
  244. }
  245. NSOperation *operation = [NSOperation new];
  246. dispatch_async(self.ioQueue, ^{
  247. if (operation.isCancelled) {
  248. return;
  249. }
  250. @autoreleasepool {
  251. UIImage *diskImage = [self diskImageForKey:key];
  252. if (diskImage) {
  253. CGFloat cost = diskImage.size.height * diskImage.size.width * diskImage.scale * diskImage.scale;
  254. [self.memCache setObject:diskImage forKey:key cost:cost];
  255. }
  256. dispatch_async(dispatch_get_main_queue(), ^{
  257. doneBlock(diskImage, SDImageCacheTypeDisk);
  258. });
  259. }
  260. });
  261. return operation;
  262. }
  263. - (void)removeImageForKey:(NSString *)key {
  264. [self removeImageForKey:key withCompletion:nil];
  265. }
  266. - (void)removeImageForKey:(NSString *)key withCompletion:(SDWebImageNoParamsBlock)completion {
  267. [self removeImageForKey:key fromDisk:YES withCompletion:completion];
  268. }
  269. - (void)removeImageForKey:(NSString *)key fromDisk:(BOOL)fromDisk {
  270. [self removeImageForKey:key fromDisk:fromDisk withCompletion:nil];
  271. }
  272. - (void)removeImageForKey:(NSString *)key fromDisk:(BOOL)fromDisk withCompletion:(SDWebImageNoParamsBlock)completion {
  273. if (key == nil) {
  274. return;
  275. }
  276. [self.memCache removeObjectForKey:key];
  277. if (fromDisk) {
  278. dispatch_async(self.ioQueue, ^{
  279. [_fileManager removeItemAtPath:[self defaultCachePathForKey:key] error:nil];
  280. if (completion) {
  281. dispatch_async(dispatch_get_main_queue(), ^{
  282. completion();
  283. });
  284. }
  285. });
  286. } else if (completion){
  287. completion();
  288. }
  289. }
  290. - (void)setMaxMemoryCost:(NSUInteger)maxMemoryCost {
  291. self.memCache.totalCostLimit = maxMemoryCost;
  292. }
  293. - (NSUInteger)maxMemoryCost {
  294. return self.memCache.totalCostLimit;
  295. }
  296. - (void)clearMemory {
  297. [self.memCache removeAllObjects];
  298. }
  299. - (void)clearDisk {
  300. [self clearDiskOnCompletion:nil];
  301. }
  302. - (void)clearDiskOnCompletion:(SDWebImageNoParamsBlock)completion
  303. {
  304. dispatch_async(self.ioQueue, ^{
  305. [_fileManager removeItemAtPath:self.diskCachePath error:nil];
  306. [_fileManager createDirectoryAtPath:self.diskCachePath
  307. withIntermediateDirectories:YES
  308. attributes:nil
  309. error:NULL];
  310. if (completion) {
  311. dispatch_async(dispatch_get_main_queue(), ^{
  312. completion();
  313. });
  314. }
  315. });
  316. }
  317. - (void)cleanDisk {
  318. [self cleanDiskWithCompletionBlock:nil];
  319. }
  320. - (void)cleanDiskWithCompletionBlock:(SDWebImageNoParamsBlock)completionBlock {
  321. dispatch_async(self.ioQueue, ^{
  322. NSURL *diskCacheURL = [NSURL fileURLWithPath:self.diskCachePath isDirectory:YES];
  323. NSArray *resourceKeys = @[NSURLIsDirectoryKey, NSURLContentModificationDateKey, NSURLTotalFileAllocatedSizeKey];
  324. // This enumerator prefetches useful properties for our cache files.
  325. NSDirectoryEnumerator *fileEnumerator = [_fileManager enumeratorAtURL:diskCacheURL
  326. includingPropertiesForKeys:resourceKeys
  327. options:NSDirectoryEnumerationSkipsHiddenFiles
  328. errorHandler:NULL];
  329. NSDate *expirationDate = [NSDate dateWithTimeIntervalSinceNow:-self.maxCacheAge];
  330. NSMutableDictionary *cacheFiles = [NSMutableDictionary dictionary];
  331. NSUInteger currentCacheSize = 0;
  332. // Enumerate all of the files in the cache directory. This loop has two purposes:
  333. //
  334. // 1. Removing files that are older than the expiration date.
  335. // 2. Storing file attributes for the size-based cleanup pass.
  336. NSMutableArray *urlsToDelete = [[NSMutableArray alloc] init];
  337. for (NSURL *fileURL in fileEnumerator) {
  338. NSDictionary *resourceValues = [fileURL resourceValuesForKeys:resourceKeys error:NULL];
  339. // Skip directories.
  340. if ([resourceValues[NSURLIsDirectoryKey] boolValue]) {
  341. continue;
  342. }
  343. // Remove files that are older than the expiration date;
  344. NSDate *modificationDate = resourceValues[NSURLContentModificationDateKey];
  345. if ([[modificationDate laterDate:expirationDate] isEqualToDate:expirationDate]) {
  346. [urlsToDelete addObject:fileURL];
  347. continue;
  348. }
  349. // Store a reference to this file and account for its total size.
  350. NSNumber *totalAllocatedSize = resourceValues[NSURLTotalFileAllocatedSizeKey];
  351. currentCacheSize += [totalAllocatedSize unsignedIntegerValue];
  352. [cacheFiles setObject:resourceValues forKey:fileURL];
  353. }
  354. for (NSURL *fileURL in urlsToDelete) {
  355. [_fileManager removeItemAtURL:fileURL error:nil];
  356. }
  357. // If our remaining disk cache exceeds a configured maximum size, perform a second
  358. // size-based cleanup pass. We delete the oldest files first.
  359. if (self.maxCacheSize > 0 && currentCacheSize > self.maxCacheSize) {
  360. // Target half of our maximum cache size for this cleanup pass.
  361. const NSUInteger desiredCacheSize = self.maxCacheSize / 2;
  362. // Sort the remaining cache files by their last modification time (oldest first).
  363. NSArray *sortedFiles = [cacheFiles keysSortedByValueWithOptions:NSSortConcurrent
  364. usingComparator:^NSComparisonResult(id obj1, id obj2) {
  365. return [obj1[NSURLContentModificationDateKey] compare:obj2[NSURLContentModificationDateKey]];
  366. }];
  367. // Delete files until we fall below our desired cache size.
  368. for (NSURL *fileURL in sortedFiles) {
  369. if ([_fileManager removeItemAtURL:fileURL error:nil]) {
  370. NSDictionary *resourceValues = cacheFiles[fileURL];
  371. NSNumber *totalAllocatedSize = resourceValues[NSURLTotalFileAllocatedSizeKey];
  372. currentCacheSize -= [totalAllocatedSize unsignedIntegerValue];
  373. if (currentCacheSize < desiredCacheSize) {
  374. break;
  375. }
  376. }
  377. }
  378. }
  379. if (completionBlock) {
  380. dispatch_async(dispatch_get_main_queue(), ^{
  381. completionBlock();
  382. });
  383. }
  384. });
  385. }
  386. - (void)backgroundCleanDisk {
  387. /***** BEGIN THREEMA MODIFICATION: use patch from github head to avoid extension restrictions *********/
  388. Class UIApplicationClass = NSClassFromString(@"UIApplication");
  389. if(!UIApplicationClass || ![UIApplicationClass respondsToSelector:@selector(sharedApplication)]) {
  390. return;
  391. }
  392. UIApplication *application = [UIApplication performSelector:@selector(sharedApplication)];
  393. /***** END THREEMA MODIFICATION *********/
  394. __block UIBackgroundTaskIdentifier bgTask = [application beginBackgroundTaskWithExpirationHandler:^{
  395. // Clean up any unfinished task business by marking where you
  396. // stopped or ending the task outright.
  397. [application endBackgroundTask:bgTask];
  398. bgTask = UIBackgroundTaskInvalid;
  399. }];
  400. // Start the long-running task and return immediately.
  401. [self cleanDiskWithCompletionBlock:^{
  402. [application endBackgroundTask:bgTask];
  403. bgTask = UIBackgroundTaskInvalid;
  404. }];
  405. }
  406. - (NSUInteger)getSize {
  407. __block NSUInteger size = 0;
  408. dispatch_sync(self.ioQueue, ^{
  409. NSDirectoryEnumerator *fileEnumerator = [_fileManager enumeratorAtPath:self.diskCachePath];
  410. for (NSString *fileName in fileEnumerator) {
  411. NSString *filePath = [self.diskCachePath stringByAppendingPathComponent:fileName];
  412. NSDictionary *attrs = [[NSFileManager defaultManager] attributesOfItemAtPath:filePath error:nil];
  413. size += [attrs fileSize];
  414. }
  415. });
  416. return size;
  417. }
  418. - (NSUInteger)getDiskCount {
  419. __block NSUInteger count = 0;
  420. dispatch_sync(self.ioQueue, ^{
  421. NSDirectoryEnumerator *fileEnumerator = [_fileManager enumeratorAtPath:self.diskCachePath];
  422. count = [[fileEnumerator allObjects] count];
  423. });
  424. return count;
  425. }
  426. - (void)calculateSizeWithCompletionBlock:(SDWebImageCalculateSizeBlock)completionBlock {
  427. NSURL *diskCacheURL = [NSURL fileURLWithPath:self.diskCachePath isDirectory:YES];
  428. dispatch_async(self.ioQueue, ^{
  429. NSUInteger fileCount = 0;
  430. NSUInteger totalSize = 0;
  431. NSDirectoryEnumerator *fileEnumerator = [_fileManager enumeratorAtURL:diskCacheURL
  432. includingPropertiesForKeys:@[NSFileSize]
  433. options:NSDirectoryEnumerationSkipsHiddenFiles
  434. errorHandler:NULL];
  435. for (NSURL *fileURL in fileEnumerator) {
  436. NSNumber *fileSize;
  437. [fileURL getResourceValue:&fileSize forKey:NSURLFileSizeKey error:NULL];
  438. totalSize += [fileSize unsignedIntegerValue];
  439. fileCount += 1;
  440. }
  441. if (completionBlock) {
  442. dispatch_async(dispatch_get_main_queue(), ^{
  443. completionBlock(fileCount, totalSize);
  444. });
  445. }
  446. });
  447. }
  448. @end