-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathImageCacheQueue.m
More file actions
executable file
·167 lines (137 loc) · 4.72 KB
/
ImageCacheQueue.m
File metadata and controls
executable file
·167 lines (137 loc) · 4.72 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
//
// ImageCacheQueue.m
// AImageDownloader
//
// Created by Jason Lee on 12-3-9.
// Copyright (c) 2012年 Taobao. All rights reserved.
//
#import "ImageCacheQueue.h"
#import "NSString+MD5.h"
static ImageCacheQueue *sharedCacheQueue = nil;
@implementation ImageCacheQueue
- (void)dealloc
{
[memoryCache release], memoryCache = nil;
[diskCachePath release], diskCachePath = nil;
//
[super dealloc];
}
- (id)init
{
self = [super init];
if (nil != sharedCacheQueue) {
//
} else {
memoryCache = [[NSMutableDictionary alloc] init];
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
diskCachePath = [[[paths objectAtIndex:0] stringByAppendingPathComponent:@"ImageCache"] retain];
if (![[NSFileManager defaultManager] fileExistsAtPath:diskCachePath]) {
NSError *error = nil;
[[NSFileManager defaultManager] createDirectoryAtPath:diskCachePath
withIntermediateDirectories:YES
attributes:nil
error:&error];
}
sharedCacheQueue = self;
}
return sharedCacheQueue;
}
+ (id)sharedCache
{
@synchronized (self) {
if (nil == sharedCacheQueue) {
sharedCacheQueue = [[ImageCacheQueue alloc] init];
}
return sharedCacheQueue;
}
}
/* Interface for the delegate */
- (UIImage *)tryToHitImageWithKey:(NSString *)key
{
UIImage *image = nil;
image = [self performSelector:@selector(getImageFromCacheByKey:) withObject:key];
return (nil != image) ? image : [self performSelector:@selector(getImageFromDiskByKey:) withObject:key];
}
- (void)cacheImage:(UIImage *)image withKey:(NSString *)key
{
NSDictionary *info = [NSDictionary dictionaryWithObjectsAndKeys:image, @"image", key, @"key", nil];
[self performSelector:@selector(cacheImageToMemory:) withObject:info];
[self performSelector:@selector(cacheImageToDisk:) withObject:info];
}
- (void)clearCache
{
[self performSelector:@selector(clearMemoryCache)];
[self performSelector:@selector(clearDiskCache)];
}
/* Real Cache Hitting */
- (UIImage *)getImageFromCacheByKey:(NSDictionary *)key
{
//return [memoryCache objectForKey:key];
if ([memoryCache objectForKey:key]) {
#ifdef DEBUG
NSLog(@"%@ was hit in memory cache.\n", key);
#endif
return [memoryCache objectForKey:key];
}
return nil;
}
- (UIImage *)getImageFromDiskByKey:(NSString *)key
{
NSString *localPath = [diskCachePath stringByAppendingPathComponent:[key MD5]];
if (![[NSFileManager defaultManager] fileExistsAtPath:localPath]) {
return nil;
}
UIImage *image = [[[UIImage alloc] initWithContentsOfFile:localPath] autorelease];
//return (nil == image) ? nil : image;
if (nil != image) {
#ifdef DEBUG
NSLog(@"%@ was hit in disk cache.\n", key);
#endif
/* Hitting here means missing in memory cache */
NSDictionary *info = [NSDictionary dictionaryWithObjectsAndKeys:image, @"image", key, @"key", nil];
[self performSelector:@selector(cacheImageToMemory:) withObject:info];
return image;
}
return nil;
}
/* Cache The Miss Image */
- (void)cacheImageToMemory:(NSDictionary *)info
{
/* What size is suitable for memoryCache ? */
/* FIFO Schedule or LRU ? */
[memoryCache setObject:[info objectForKey:@"image"] forKey:[info objectForKey:@"key"]];
#ifdef DEBUG
NSLog(@"%@ was cached.\n", [info objectForKey:@"key"]);
#endif
}
- (void)cacheImageToDisk:(NSDictionary *)info
{
NSString *key = [info objectForKey:@"key"];
UIImage *image = [info objectForKey:@"image"];
NSString *localPath = [diskCachePath stringByAppendingPathComponent:[key MD5]];
NSData *localData = UIImageJPEGRepresentation(image, 1.0f);
if ([localData length] <= 1) {
return ;
}
if (![[NSFileManager defaultManager] fileExistsAtPath:localPath]) {
[[NSFileManager defaultManager] createFileAtPath:localPath contents:localData attributes:nil];
}
#ifdef DEBUG
NSLog(@"%@ was saved to disk %@.\n", key, localPath);
#endif
}
/* Empty The Cache */
- (void)clearMemoryCache
{
[memoryCache removeAllObjects];
}
- (void)clearDiskCache
{
NSError *error = nil;
[[NSFileManager defaultManager] removeItemAtPath:diskCachePath error:&error];
[[NSFileManager defaultManager] createDirectoryAtPath:diskCachePath
withIntermediateDirectories:YES
attributes:nil
error:&error];
}
@end