@@ -30,7 +30,7 @@ @interface JMImageCache ()
30
30
31
31
@property (strong , nonatomic ) NSOperationQueue *diskOperationQueue;
32
32
33
- - (void ) _downloadAndWriteImageForURL : (NSURL *)url key : (NSString *)key completionBlock : (void (^)(UIImage *image))completion ;
33
+ - (void ) _downloadAndWriteImageForURL : (NSURL *)url key : (NSString *)key completionBlock : (void (^)(UIImage *image))completion failureBlock : ( void (^)( NSURLRequest *request, NSURLResponse *response, NSError * error)) failure ;
34
34
35
35
@end
36
36
@@ -62,33 +62,58 @@ - (id) init {
62
62
return self;
63
63
}
64
64
65
- - (void ) _downloadAndWriteImageForURL : (NSURL *)url key : (NSString *)key completionBlock : (void (^)(UIImage *image))completion {
65
+ - (void ) _downloadAndWriteImageForURL : (NSURL *)url key : (NSString *)key completionBlock : (void (^)(UIImage *image))completion failureBlock : (void (^)(NSURLRequest *request, NSURLResponse *response, NSError * error))failure
66
+ {
66
67
if (!key && !url) return ;
67
68
68
69
if (!key) {
69
70
key = keyForURL (url);
70
71
}
71
72
72
73
dispatch_async (dispatch_get_global_queue (DISPATCH_QUEUE_PRIORITY_DEFAULT, 0 ), ^{
73
- NSData *data = [NSData dataWithContentsOfURL: url];
74
- UIImage *i = [[UIImage alloc ] initWithData: data];
75
- // stop process if the method could not initialize the image from the specified data
76
- if (!i) return ;
77
74
78
- NSString *cachePath = cachePathForKey (key);
79
- NSInvocation *writeInvocation = [NSInvocation invocationWithMethodSignature: [self methodSignatureForSelector: @selector (writeData:toPath: )]];
80
-
81
- [writeInvocation setTarget: self ];
82
- [writeInvocation setSelector: @selector (writeData:toPath: )];
83
- [writeInvocation setArgument: &data atIndex: 2 ];
84
- [writeInvocation setArgument: &cachePath atIndex: 3 ];
85
-
86
- [self performDiskWriteOperation: writeInvocation];
87
- [self setImage: i forKey: key];
88
-
89
- dispatch_async (dispatch_get_main_queue (), ^{
90
- if (completion) completion (i);
91
- });
75
+ NSURLRequest * request = [NSURLRequest requestWithURL: url];
76
+ NSURLResponse * response = nil ;
77
+ NSError * error = nil ;
78
+ NSData * data = [NSURLConnection sendSynchronousRequest: request returningResponse: &response error: &error];
79
+
80
+ if (error)
81
+ {
82
+ dispatch_async (dispatch_get_main_queue (), ^{
83
+
84
+ if (failure) failure (request, response, error);
85
+ });
86
+ return ;
87
+ }
88
+
89
+ UIImage *i = [[UIImage alloc ] initWithData: data];
90
+ if (!i)
91
+ {
92
+ NSMutableDictionary *errorDetail = [NSMutableDictionary dictionary ];
93
+ [errorDetail setValue: [NSString stringWithFormat: @" Failed to init image with data from for URL: %@ " , url] forKey: NSLocalizedDescriptionKey ];
94
+ NSError * error = [NSError errorWithDomain: @" JMImageCacheErrorDomain" code: 1 userInfo: errorDetail];
95
+ dispatch_async (dispatch_get_main_queue (), ^{
96
+
97
+ if (failure) failure (request, response, error);
98
+ });
99
+ }
100
+ else
101
+ {
102
+ NSString *cachePath = cachePathForKey (key);
103
+ NSInvocation *writeInvocation = [NSInvocation invocationWithMethodSignature: [self methodSignatureForSelector: @selector (writeData:toPath: )]];
104
+
105
+ [writeInvocation setTarget: self ];
106
+ [writeInvocation setSelector: @selector (writeData:toPath: )];
107
+ [writeInvocation setArgument: &data atIndex: 2 ];
108
+ [writeInvocation setArgument: &cachePath atIndex: 3 ];
109
+
110
+ [self performDiskWriteOperation: writeInvocation];
111
+ [self setImage: i forKey: key];
112
+
113
+ dispatch_async (dispatch_get_main_queue (), ^{
114
+ if (completion) completion (i);
115
+ });
116
+ }
92
117
});
93
118
}
94
119
@@ -133,19 +158,19 @@ - (void) removeObjectForKey:(id)key {
133
158
#pragma mark -
134
159
#pragma mark Getter Methods
135
160
136
- - (void ) imageForURL : (NSURL *)url key : (NSString *)key completionBlock : (void (^)(UIImage *image))completion {
161
+ - (void ) imageForURL : (NSURL *)url key : (NSString *)key completionBlock : (void (^)(UIImage *image))completion failureBlock : ( void (^)( NSURLRequest *request, NSURLResponse *response, NSError * error)) failure {
137
162
138
163
UIImage *i = [self cachedImageForKey: key];
139
164
140
165
if (i) {
141
166
if (completion) completion (i);
142
167
} else {
143
- [self _downloadAndWriteImageForURL: url key: key completionBlock: completion];
168
+ [self _downloadAndWriteImageForURL: url key: key completionBlock: completion failureBlock: failure ];
144
169
}
145
170
}
146
171
147
- - (void ) imageForURL : (NSURL *)url completionBlock : (void (^)(UIImage *image))completion {
148
- [self imageForURL: url key: keyForURL (url) completionBlock: completion];
172
+ - (void ) imageForURL : (NSURL *)url completionBlock : (void (^)(UIImage *image))completion failureBlock : ( void (^)( NSURLRequest *request, NSURLResponse *response, NSError * error)) failure {
173
+ [self imageForURL: url key: keyForURL (url) completionBlock: completion failureBlock: (failure) ];
149
174
}
150
175
151
176
- (UIImage *) cachedImageForKey : (NSString *)key {
@@ -187,7 +212,8 @@ - (UIImage *) imageForURL:(NSURL *)url key:(NSString*)key delegate:(id<JMImageCa
187
212
[d cache: self didDownloadImage: image forURL: url key: key];
188
213
}
189
214
}
190
- }];
215
+ }
216
+ failureBlock: nil ];
191
217
}
192
218
193
219
return nil ;
0 commit comments