This repository was archived by the owner on Aug 10, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathKVImageCache.m
More file actions
202 lines (168 loc) · 8.39 KB
/
KVImageCache.m
File metadata and controls
202 lines (168 loc) · 8.39 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
//
// KVImageCache.m
// Koolistov
//
// Created by Johan Kool on 28-10-10.
// Copyright 2010-2011 Koolistov. All rights reserved.
//
// Redistribution and use in source and binary forms, with or without modification, are
// permitted provided that the following conditions are met:
//
// * Redistributions of source code must retain the above copyright notice, this list of
// conditions and the following disclaimer.
// * Redistributions in binary form must reproduce the above copyright notice, this list
// of conditions and the following disclaimer in the documentation and/or other materials
// provided with the distribution.
// * Neither the name of KOOLISTOV nor the names of its contributors may be used to
// endorse or promote products derived from this software without specific prior written
// permission.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY
// EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
// MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
// THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
// OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
// HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
// OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
// SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
//
#import "KVImageCache.h"
#import "KVDownload.h"
@interface KVImageCache ()
@property (retain) NSMutableDictionary *imagesLoading;
@property (retain) NSMutableDictionary *downloadPerImageView;
@end
@implementation KVImageCache
@synthesize imageURLCache = imageURLCache_;
@synthesize imagesLoading = imagesLoading_;
@synthesize downloadPerImageView = downloadPerImageView_;
@synthesize shouldCheckForLocalImages=shouldCheckForLocalImages_;
+ (KVImageCache *)defaultCache {
static dispatch_once_t pred;
static KVImageCache *defaultCache = nil;
dispatch_once(&pred, ^{ defaultCache = [[self alloc] init]; });
return defaultCache;
}
- (id)init {
self = [super init];
if (self) {
// Create cache with 1 MB memory and 10 MB disk capacity
// Using SDURLCache subclass which enables caching to disk
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
NSString *diskCachePath = [[paths objectAtIndex:0] stringByAppendingPathComponent:@"KVImageCache"];
NSURLCache *cache = [[NSURLCache alloc] initWithMemoryCapacity:1 * 1024 * 1024 diskCapacity:10 * 1024 * 1024 diskPath:diskCachePath];
self.imageURLCache = cache;
[cache release];
self.imagesLoading = [NSMutableDictionary dictionary];
self.downloadPerImageView = [NSMutableDictionary dictionary];
self.shouldCheckForLocalImages = NO;
}
return self;
}
- (void)dealloc {
self.imageURLCache = nil;
self.imagesLoading = nil;
[super dealloc];
}
#pragma mark - Main
- (id)loadImageAtURL:(NSURL *)imageURL withHandler:(void (^)(UIImage * image))handler {
return [self loadImageAtURL:imageURL cacheURL:imageURL withHandler:handler];
}
- (id)loadImageAtURL:(NSURL *)imageURL cacheURL:(NSURL *)cacheURL withHandler:(void (^)(UIImage * image))handler {
return [self loadImageAtURL:imageURL cacheURL:cacheURL imageView:nil withHandler:handler];
}
- (id)loadImageAtURL:(NSURL *)imageURL cacheURL:(NSURL *)cacheURL imageView:(UIImageView *)imageView withHandler:(void (^)(UIImage * image))handler {
if (!imageURL) {
handler(nil);
return nil;
}
// Check if a local image is referenced
if (self.shouldCheckForLocalImages) {
UIImage *localImage = [UIImage imageNamed:[imageURL absoluteString]];
if (localImage) {
handler(localImage);
return nil;
}
}
NSURLRequest *cacheRequest = [NSURLRequest requestWithURL:cacheURL];
NSURLRequest *downloadRequest = [NSURLRequest requestWithURL:imageURL];
NSCachedURLResponse *earlierCachedResponse = [self.imageURLCache cachedResponseForRequest:cacheRequest];
if (earlierCachedResponse) {
UIImage *image = [UIImage imageWithData:[earlierCachedResponse data]];
handler(image);
return nil;
} else {
NSMutableDictionary *pendingHandlers = [self.imagesLoading objectForKey:cacheURL];
// UIImageView doesn't comply to NSCopying protocol, its pointer shouldn't change and is unique
NSString *key = [NSString stringWithFormat:@"%p", imageView];
if (pendingHandlers) {
[pendingHandlers setObject:[[handler copy] autorelease] forKey:key];
return nil;
} else {
pendingHandlers = [NSMutableDictionary dictionaryWithObject:[[handler copy] autorelease] forKey:key];
[self.imagesLoading setObject:pendingHandlers forKey:cacheURL];
KVDownload *imageDownload = [KVDownload startDownloadWithRequest:downloadRequest completionHandler:^(NSURLResponse * receivedResponse, NSData * data, NSError * error) {
if (!data || [data length] == 0) {
// If no data, return nil image
NSMutableDictionary *pendingHandlers = [self.imagesLoading objectForKey:cacheURL];
for (void (^handler)(UIImage * image) in [pendingHandlers allValues]) {
handler (nil);
}
[self.imagesLoading removeObjectForKey:cacheURL];
} else {
// Return image
UIImage *image = [UIImage imageWithData:data];
if (image) {
// Store data in cache
NSCachedURLResponse *cachedResponse = [[NSCachedURLResponse alloc] initWithResponse:receivedResponse data:data];
[self.imageURLCache storeCachedResponse:cachedResponse forRequest:cacheRequest];
[cachedResponse release];
}
NSMutableDictionary *pendingHandlers = [self.imagesLoading objectForKey:cacheURL];
for (void (^handler)(UIImage * image) in [pendingHandlers allValues]) {
handler (image);
}
[self.imagesLoading removeObjectForKey:cacheURL];
}
[self.downloadPerImageView removeObjectForKey:key];
}];
[self.downloadPerImageView setObject:[NSDictionary dictionaryWithObjectsAndKeys:imageDownload, @"download", cacheURL, @"cacheURL", nil] forKey:key];
return imageDownload;
}
}
}
- (void)cancelDownloadForImageView:(UIImageView *)imageView {
NSString *key = [NSString stringWithFormat:@"%p", imageView];
NSDictionary *downloadDict = [[[self.downloadPerImageView objectForKey:key] retain] autorelease];
if (downloadDict) {
NSURL *cacheURL = [downloadDict objectForKey:@"cacheURL"];
// Check if there are other pendingHandlers for this URL
NSMutableDictionary *pendingHandlers = [self.imagesLoading objectForKey:cacheURL];
// Remove handler for this imageView
[pendingHandlers removeObjectForKey:key];
// Cancel download only if no more pending handlers remain
if ([pendingHandlers count] == 0) {
KVDownload *download = [downloadDict objectForKey:@"download"];
[download cancel];
[self.imagesLoading removeObjectForKey:cacheURL];
}
[self.downloadPerImageView removeObjectForKey:key];
}
}
- (UIImage *)cachedImageAtURL:(NSURL *)cacheURL {
// Check if a local image is referenced
if (self.shouldCheckForLocalImages) {
UIImage *localImage = [UIImage imageNamed:[cacheURL absoluteString]];
if (localImage) {
return localImage;
}
}
NSURLRequest *request = [NSURLRequest requestWithURL:cacheURL];
NSCachedURLResponse *earlierCachedResponse = [self.imageURLCache cachedResponseForRequest:request];
return [UIImage imageWithData:[earlierCachedResponse data]];
}
- (void)flush {
[self.imageURLCache removeAllCachedResponses];
}
@end