This repository has been archived by the owner on May 8, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
DCTImageCache.m
173 lines (139 loc) · 4.9 KB
/
DCTImageCache.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
//
// DCTImageCache.m
// DCTImageCache
//
// Created by Daniel Tull on 25.05.2012.
// Copyright (c) 2012 Daniel Tull Limited. All rights reserved.
//
#import "DCTImageCache.h"
#import "_DCTImageCacheDiskCache.h"
#import "_DCTImageCacheMemoryCache.h"
#import "_DCTImageCacheFetcher.h"
#import "_DCTImageCacheProcessManager.h"
#import "_DCTImageCacheOperation.h"
NSString *const DCTImageCacheBundleName = @"DCTImageCache.bundle";
NSString *const DCTImageCacheDefaultCacheName = @"DCTDefaultImageCache";
@implementation DCTImageCache {
_DCTImageCacheMemoryCache *_memoryCache;
_DCTImageCacheDiskCache *_diskCache;
_DCTImageCacheFetcher *_fetcher;
}
#pragma mark DCTImageCache
+ (NSMutableDictionary *)imageCaches {
static NSMutableDictionary *sharedInstance = nil;
static dispatch_once_t sharedToken;
dispatch_once(&sharedToken, ^{
sharedInstance = [NSMutableDictionary new];
});
return sharedInstance;
}
+ (instancetype)defaultImageCache {
return [self imageCacheWithName:DCTImageCacheDefaultCacheName];
}
+ (instancetype)imageCacheWithName:(NSString *)name {
NSURL *URL = [[self _defaultDirectory] URLByAppendingPathComponent:name];
return [self imageCacheWithURL:URL];
}
+ (instancetype)imageCacheWithURL:(NSURL *)storeURL {
NSMutableDictionary *imageCaches = [self imageCaches];
DCTImageCache *imageCache = [imageCaches objectForKey:storeURL];
if (!imageCache) {
imageCache = [[self alloc] _initWithStoreURL:storeURL];
[imageCaches setObject:imageCache forKey:storeURL];
}
return imageCache;
}
- (id)_initWithStoreURL:(NSURL *)storeURL {
self = [self init];
if (!self) return nil;
_diskCache = [[_DCTImageCacheDiskCache alloc] initWithStoreURL:storeURL];
_fetcher = [_DCTImageCacheFetcher new];
_name = [[storeURL lastPathComponent] copy];
_memoryCache = [_DCTImageCacheMemoryCache new];
return self;
}
- (NSURL *)storeURL {
return _diskCache.storeURL;
}
- (void)setImageFetcher:(DCTImageCacheFetcher)imageFetcher {
_fetcher.imageFetcher = imageFetcher;
}
- (DCTImageCacheFetcher)imageFetcher {
return _fetcher.imageFetcher;
}
- (void)removeAllImages {
[_memoryCache removeAllImages];
[_diskCache removeAllImages];
}
- (void)removeImagesWithAttributes:(DCTImageCacheAttributes *)attributes {
[_memoryCache removeImagesWithAttributes:attributes];
[_diskCache removeImagesWithAttributes:attributes];
}
- (void)prefetchImageWithAttributes:(DCTImageCacheAttributes *)attributes {
[_diskCache hasImageWithAttributes:attributes handler:^(BOOL hasImage, NSError *error) {
if (hasImage) return;
[_fetcher fetchImageWithAttributes:attributes handler:^(UIImage *image, NSError *error) {
[_diskCache setImage:image forAttributes:attributes];
}];
}];
}
- (id<DCTImageCacheProcess>)fetchImageWithAttributes:(DCTImageCacheAttributes *)attributes
handler:(DCTImageCacheImageHandler)handler {
if (handler == NULL) {
[self prefetchImageWithAttributes:attributes];
return nil;
}
// If the image exists in the memory cache, use it!
UIImage *image = [_memoryCache imageWithAttributes:attributes];
if (image) {
handler(image, nil);
return nil;
}
// If the image is in the disk queue to be saved, pull it out and use it
image = [_diskCache imageWithAttributes:attributes];
if (image) {
handler(image, nil);
return nil;
}
_DCTImageCacheCancelProxy *cancelProxy = [_DCTImageCacheCancelProxy new];
cancelProxy.imageHandler = handler;
_DCTImageCacheProcessManager *processManager = [_DCTImageCacheProcessManager new];
[processManager addCancelProxy:cancelProxy];
processManager.process = [_diskCache fetchImageWithAttributes:attributes handler:^(UIImage *image, NSError *error) {
if (image) {
[_memoryCache setImage:image forAttributes:attributes];
[processManager setImage:image];
return;
}
processManager.process = [_fetcher fetchImageWithAttributes:attributes handler:^(UIImage *image, NSError *error) {
[processManager setImage:image];
[processManager setError:error];
if (!image) return;
[_memoryCache setImage:image forAttributes:attributes];
[_diskCache setImage:image forAttributes:attributes];
}];
}];
return cancelProxy;
}
+ (NSURL *)cacheDirectoryURL {
return [[[NSFileManager defaultManager] URLsForDirectory:NSCachesDirectory inDomains:NSUserDomainMask] lastObject];
}
#pragma mark Internal
+ (NSBundle *)_bundle {
static NSBundle *bundle;
static dispatch_once_t bundleToken;
dispatch_once(&bundleToken, ^{
NSDirectoryEnumerator *enumerator = [[NSFileManager new] enumeratorAtURL:[[NSBundle mainBundle] bundleURL]
includingPropertiesForKeys:nil
options:NSDirectoryEnumerationSkipsHiddenFiles
errorHandler:NULL];
for (NSURL *URL in enumerator)
if ([[URL lastPathComponent] isEqualToString:DCTImageCacheBundleName])
bundle = [NSBundle bundleWithURL:URL];
});
return bundle;
}
+ (NSURL *)_defaultDirectory {
return [[self cacheDirectoryURL] URLByAppendingPathComponent:NSStringFromClass(self)];
}
@end