@@ -30,7 +30,7 @@ @interface JMImageCache ()
3030
3131@property (strong , nonatomic ) NSOperationQueue *diskOperationQueue;
3232
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 ;
3434
3535@end
3636
@@ -62,33 +62,58 @@ - (id) init {
6262 return self;
6363}
6464
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+ {
6667 if (!key && !url) return ;
6768
6869 if (!key) {
6970 key = keyForURL (url);
7071 }
7172
7273 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 ;
7774
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+ }
92117 });
93118}
94119
@@ -133,19 +158,19 @@ - (void) removeObjectForKey:(id)key {
133158#pragma mark -
134159#pragma mark Getter Methods
135160
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 {
137162
138163 UIImage *i = [self cachedImageForKey: key];
139164
140165 if (i) {
141166 if (completion) completion (i);
142167 } else {
143- [self _downloadAndWriteImageForURL: url key: key completionBlock: completion];
168+ [self _downloadAndWriteImageForURL: url key: key completionBlock: completion failureBlock: failure ];
144169 }
145170}
146171
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) ];
149174}
150175
151176- (UIImage *) cachedImageForKey : (NSString *)key {
@@ -187,7 +212,8 @@ - (UIImage *) imageForURL:(NSURL *)url key:(NSString*)key delegate:(id<JMImageCa
187212 [d cache: self didDownloadImage: image forURL: url key: key];
188213 }
189214 }
190- }];
215+ }
216+ failureBlock: nil ];
191217 }
192218
193219 return nil ;
0 commit comments