CNLiveImageCache.m
7.43 KB
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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
//
// CNLiveImageCache.m
// WSWebImageDemo
//
// Created by 王帅 on 16/7/19.
// Copyright © 2016年 王帅. All rights reserved.
//
#import "CNLiveImageCache.h"
#import <CommonCrypto/CommonDigest.h>
#import "UIImage+MultiFormat.h"
FOUNDATION_STATIC_INLINE NSUInteger CNLiveCacheCostForImage(UIImage *image) {
return image.size.height * image.size.width * image.scale * image.scale;
}
typedef void(^CNLiveWebImageNoParamsBlock)();
@interface CNLiveImageCache ()
@property (nonatomic, strong) NSCache *memoryCache;
@property (nonatomic, copy) NSString *diskCachePath;
@property (nonatomic, strong) dispatch_queue_t ioQueue;
@end
@implementation CNLiveImageCache
{
NSFileManager *_fileManager;
}
+ (CNLiveImageCache *)sharedImageCache
{
static dispatch_once_t onceToken;
static id instance;
dispatch_once(&onceToken, ^{
instance = [self new];
});
return instance;
}
- (id)init {
return [self initWithNamespace:@"default"];
}
- (id)initWithNamespace:(NSString *)ns {
NSString *path = [self makeDiskCachePath:ns];
return [self initWithNamespace:ns diskCacheDirectory:path];
}
- (NSString *)makeDiskCachePath:(NSString *)fullNamespace {
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
return [paths[0] stringByAppendingString:fullNamespace];
}
- (id)initWithNamespace:(NSString *)ns diskCacheDirectory:(NSString *)directory {
if (self = [super init]) {
NSString *fullNameSpace = [@"com.cnlive.CNLiveImageCache." stringByAppendingString:ns];
_ioQueue = dispatch_queue_create("com.cnlive.CNLiveImageCacheQueue", DISPATCH_QUEUE_SERIAL);
_memoryCache = [[NSCache alloc] init];
_memoryCache.name = fullNameSpace;
if (directory != nil) {
_diskCachePath = [directory stringByAppendingPathComponent:fullNameSpace];
}
else {
_diskCachePath = [[self makeDiskCachePath:ns] stringByAppendingPathComponent:fullNameSpace];
}
dispatch_sync(_ioQueue, ^{
_fileManager = [NSFileManager new];
});
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(clearMemory)
name:UIApplicationDidReceiveMemoryWarningNotification
object:nil];
// [[NSNotificationCenter defaultCenter] addObserver:self
// selector:@selector(clearDisk)
// name:UIApplicationWillTerminateNotification
// object:nil];
}
return self;
}
- (void)dealloc
{
[[NSNotificationCenter defaultCenter] removeObserver:self];
}
- (void)storeImage:(UIImage *)image forKey:(NSString *)key toDisk:(BOOL)toDisk
{
if (!image || !key) {
return;
}
NSUInteger cost = CNLiveCacheCostForImage(image);
[_memoryCache setObject:image forKey:key cost:cost];
if (toDisk) {
dispatch_async(_ioQueue, ^{
NSData *data = UIImageJPEGRepresentation(image, 1.0);
if (data) {
if (![_fileManager fileExistsAtPath:_diskCachePath]) {
[_fileManager createDirectoryAtPath:_diskCachePath withIntermediateDirectories:YES attributes:nil error:NULL];
}
NSString *cachePathForKey = [self defaultCachePathForKey:key];
[_fileManager createFileAtPath:cachePathForKey contents:data attributes:nil];
}
});
}
}
- (UIImage *)imageFromMemoryCacheForKey:(NSString *)key
{
return [self.memoryCache objectForKey:key];
}
- (UIImage *)imageFromDiskCacheForKey:(NSString *)key
{
UIImage *memoryImage = [self imageFromMemoryCacheForKey:key];
if (memoryImage) {
return memoryImage;
}
NSData *diskData = [self getDiskImageDataWithKey:key];
if (diskData) {
return [UIImage ws_imageWithData:diskData];
}
return nil;
}
- (BOOL)queryDiskCacheForKey:(NSString *)key done:(CNLiveWebImageQueryCompletedBlock)doneBlock
{
if (!doneBlock) {
return NO;
}
if (!key) {
doneBlock(nil, CNLiveImageCacheTypeNone);
return NO;
}
UIImage *memoryImage = [self imageFromMemoryCacheForKey:key];
if (memoryImage) {
doneBlock(memoryImage, CNLiveImageCacheTypeMemory);
return YES;
}
UIImage *diskImage = [self imageFromDiskCacheForKey:key];
if (diskImage) {
doneBlock(diskImage, CNLiveImageCacheTypeDisk);
return YES;
}
doneBlock(nil, CNLiveImageCacheTypeNone);
return NO;
}
- (void)removeCacheForKey:(NSString *)key done:(CNLiveCompletedBlock)completed
{
if (!key || !completed) {
return;
}
if ([self imageFromMemoryCacheForKey:key]) {
[self.memoryCache removeObjectForKey:key];
}
if ([self imageFromDiskCacheForKey:key]) {
NSString *cachePathForKey = [self defaultCachePathForKey:key];
[_fileManager removeItemAtPath:cachePathForKey error:nil];
}
completed();
}
- (long long)diskCacheSize
{
if ([_fileManager fileExistsAtPath:self.diskCachePath]) {
NSArray *subPathArray = [_fileManager subpathsAtPath:self.diskCachePath];
if (subPathArray && subPathArray.count > 0) {
long long totalCacheSize = 0;
for (NSString *subPath in subPathArray) {
NSString *fullPath = [self.diskCachePath stringByAppendingPathComponent:subPath];
NSDictionary *cacheSizeDic = [_fileManager attributesOfItemAtPath:fullPath error:nil];
totalCacheSize += [cacheSizeDic fileSize];
}
return totalCacheSize;
}
}
return 0;
}
- (void)clearMemory
{
[self.memoryCache removeAllObjects];
}
- (void)clearDisk
{
[self clearDiskOnCompletion:nil];
}
- (void)clearDiskOnCompletion:(CNLiveWebImageNoParamsBlock)completion
{
dispatch_async(self.ioQueue, ^{
[_fileManager removeItemAtPath:self.diskCachePath error:nil];
[_fileManager createDirectoryAtPath:self.diskCachePath withIntermediateDirectories:YES attributes:nil error:NULL];
if (completion) {
dispatch_async(dispatch_get_main_queue(), ^{
completion();
});
}
});
}
#pragma mark <PrivateMethod>
- (NSData *)getDiskImageDataWithKey:(NSString *)key
{
NSString *filePath = [self defaultCachePathForKey:key];
NSData *imageData = [NSData dataWithContentsOfFile:filePath];
return imageData;
}
- (NSString *)defaultCachePathForKey:(NSString *)key
{
return [self cachePathForKey:key inPath:self.diskCachePath];
}
- (NSString *)cachePathForKey:(NSString *)key inPath:(NSString *)path
{
NSString *fileName = [self cachedFileNameForKey:key];
return [path stringByAppendingPathComponent:fileName];
}
- (NSString *)cachedFileNameForKey:(NSString *)key {
const char *str = [key UTF8String];
if (str == NULL) {
str = "";
}
unsigned char r[CC_MD5_DIGEST_LENGTH];
CC_MD5(str, (CC_LONG)strlen(str), r);
NSString *filename = [NSString stringWithFormat:@"%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x",
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]];
return filename;
}
@end