forked from jakemarsh/JMImageCache
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathJMImageCache.m
152 lines (106 loc) · 3.57 KB
/
JMImageCache.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
//
// JMImageCache.m
// JMCache
//
// Created by Jake Marsh on 2/7/11.
// Copyright 2011 Rubber Duck Software. All rights reserved.
//
#import "JMImageCache.h"
static NSString* _JMImageCacheDirectory;
static inline NSString* JMImageCacheDirectory() {
if(!_JMImageCacheDirectory) {
_JMImageCacheDirectory = [[NSHomeDirectory() stringByAppendingPathComponent:@"Library/Caches/JMCache"] copy];
}
return _JMImageCacheDirectory;
}
inline static NSString* keyForURL(NSString *url) {
return [NSString stringWithFormat:@"JMImageCache-%u", [url hash]];
}
static inline NSString* cachePathForURL(NSString* key) {
return [JMImageCacheDirectory() stringByAppendingPathComponent:keyForURL(key)];
}
JMImageCache *_sharedCache = nil;
@implementation JMImageCache
+ (JMImageCache *) sharedCache {
if(!_sharedCache) {
_sharedCache = [[JMImageCache alloc] init];
}
return _sharedCache;
}
- (id) init {
if((self = [super init])) {
_diskOperationQueue = [[NSOperationQueue alloc] init];
[[NSFileManager defaultManager] createDirectoryAtPath:JMImageCacheDirectory()
withIntermediateDirectories:YES
attributes:nil
error:NULL];
}
return self;
}
#pragma mark -
#pragma mark Getter Methods
- (UIImage *) imageForURL:(NSString *)url delegate:(id<JMImageCacheDelegate>)d {
if(!url) {
return nil;
}
id returner = [super objectForKey:url];
if(returner) {
return returner;
} else {
UIImage *i = [self imageFromDiskForURL:url];
if(i) {
[self setImage:i forURL:url];
return i;
}
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
NSData *data = [NSData dataWithContentsOfURL:[NSURL URLWithString:url]];
if (data) {
UIImage* i = [[[UIImage alloc] initWithData:data] autorelease];
if (!i) return;
NSString* cachePath = cachePathForURL(url);
NSInvocation* writeInvocation = [NSInvocation invocationWithMethodSignature:[self methodSignatureForSelector:@selector(writeData:toPath:)]];
[writeInvocation setTarget:self];
[writeInvocation setSelector:@selector(writeData:toPath:)];
[writeInvocation setArgument:&data atIndex:2];
[writeInvocation setArgument:&cachePath atIndex:3];
[self performDiskWriteOperation:writeInvocation];
[self setImage:i forURL:url];
dispatch_async(dispatch_get_main_queue(), ^{
if(d) {
if([d respondsToSelector:@selector(cache:didDownloadImage:forURL:)]) {
[d cache:self didDownloadImage:i forURL:url];
}
}
});
}
});
return nil;
}
}
- (UIImage *) imageFromDiskForURL:(NSString *)url {
UIImage *i = [[[UIImage alloc] initWithData:[NSData dataWithContentsOfFile:cachePathForURL(url) options:0 error:NULL]] autorelease];
return i;
}
#pragma mark -
#pragma mark Setter Methods
- (void) setImage:(UIImage *)i forURL:(NSString *)url {
[super setObject:i forKey:url];
}
- (void) removeImageForURL:(NSString *)url {
[super removeObjectForKey:keyForURL(url)];
}
#pragma mark -
#pragma mark Disk Writing Operations
- (void) writeData:(NSData*)data toPath:(NSString *)path {
[data writeToFile:path atomically:YES];
}
- (void) performDiskWriteOperation:(NSInvocation *)invoction {
NSInvocationOperation *operation = [[NSInvocationOperation alloc] initWithInvocation:invoction];
[_diskOperationQueue addOperation:operation];
[operation release];
}
- (void) dealloc {
[_diskOperationQueue release];
[super dealloc];
}
@end