@@ -31,7 +31,7 @@ @interface JMImageCache ()
3131
3232@property (strong , nonatomic ) NSOperationQueue *diskOperationQueue;
3333
34- - (void ) _downloadAndWriteImageForURL : (NSURL *)url key : (NSString *)key completionBlock : (void (^)(UIImage *image))completion ;
34+ - (void ) _downloadAndWriteImageForURL : (NSURL *)url key : (NSString *)key completionBlock : (void (^)(UIImage *image))completion failureBlock : ( void (^)( NSURLRequest *request, NSURLResponse *response, NSError * error)) failure ;
3535
3636@end
3737
@@ -60,33 +60,58 @@ - (id) init {
6060 return self;
6161}
6262
63- - (void ) _downloadAndWriteImageForURL : (NSURL *)url key : (NSString *)key completionBlock : (void (^)(UIImage *image))completion {
63+ - (void ) _downloadAndWriteImageForURL : (NSURL *)url key : (NSString *)key completionBlock : (void (^)(UIImage *image))completion failureBlock : (void (^)(NSURLRequest *request, NSURLResponse *response, NSError * error))failure
64+ {
6465 if (!key && !url) return ;
6566
6667 if (!key) {
6768 key = keyForURL (url);
6869 }
6970
7071 dispatch_async (dispatch_get_global_queue (DISPATCH_QUEUE_PRIORITY_DEFAULT , 0 ), ^{
71- NSData *data = [NSData dataWithContentsOfURL: url];
72- UIImage *i = [[UIImage alloc ] initWithData: data];
73- // stop process if the method could not initialize the image from the specified data
74- if (!i) return ;
7572
76- NSString *cachePath = cachePathForKey (key);
77- NSInvocation *writeInvocation = [NSInvocation invocationWithMethodSignature: [self methodSignatureForSelector: @selector (writeData:toPath: )]];
78-
79- [writeInvocation setTarget: self ];
80- [writeInvocation setSelector: @selector (writeData:toPath: )];
81- [writeInvocation setArgument: &data atIndex: 2 ];
82- [writeInvocation setArgument: &cachePath atIndex: 3 ];
83-
84- [self performDiskWriteOperation: writeInvocation];
85- [self setImage: i forKey: key];
86-
87- dispatch_async (dispatch_get_main_queue (), ^{
88- if (completion) completion (i);
89- });
73+ NSURLRequest * request = [NSURLRequest requestWithURL: url];
74+ NSURLResponse * response = nil ;
75+ NSError * error = nil ;
76+ NSData * data = [NSURLConnection sendSynchronousRequest: request returningResponse: &response error: &error];
77+
78+ if (error)
79+ {
80+ dispatch_async (dispatch_get_main_queue (), ^{
81+
82+ if (failure) failure (request, response, error);
83+ });
84+ return ;
85+ }
86+
87+ UIImage *i = [[UIImage alloc ] initWithData: data];
88+ if (!i)
89+ {
90+ NSMutableDictionary *errorDetail = [NSMutableDictionary dictionary ];
91+ [errorDetail setValue: [NSString stringWithFormat: @" Failed to init image with data from for URL: %@ " , url] forKey: NSLocalizedDescriptionKey ];
92+ NSError * error = [NSError errorWithDomain: @" JMImageCacheErrorDomain" code: 1 userInfo: errorDetail];
93+ dispatch_async (dispatch_get_main_queue (), ^{
94+
95+ if (failure) failure (request, response, error);
96+ });
97+ }
98+ else
99+ {
100+ NSString *cachePath = cachePathForKey (key);
101+ NSInvocation *writeInvocation = [NSInvocation invocationWithMethodSignature: [self methodSignatureForSelector: @selector (writeData:toPath: )]];
102+
103+ [writeInvocation setTarget: self ];
104+ [writeInvocation setSelector: @selector (writeData:toPath: )];
105+ [writeInvocation setArgument: &data atIndex: 2 ];
106+ [writeInvocation setArgument: &cachePath atIndex: 3 ];
107+
108+ [self performDiskWriteOperation: writeInvocation];
109+ [self setImage: i forKey: key];
110+
111+ dispatch_async (dispatch_get_main_queue (), ^{
112+ if (completion) completion (i);
113+ });
114+ }
90115 });
91116}
92117
@@ -131,19 +156,19 @@ - (void) removeObjectForKey:(id)key {
131156#pragma mark -
132157#pragma mark Getter Methods
133158
134- - (void ) imageForURL : (NSURL *)url key : (NSString *)key completionBlock : (void (^)(UIImage *image))completion {
159+ - (void ) imageForURL : (NSURL *)url key : (NSString *)key completionBlock : (void (^)(UIImage *image))completion failureBlock : ( void (^)( NSURLRequest *request, NSURLResponse *response, NSError * error)) failure {
135160
136161 UIImage *i = [self cachedImageForKey: key];
137162
138163 if (i) {
139164 if (completion) completion (i);
140165 } else {
141- [self _downloadAndWriteImageForURL: url key: key completionBlock: completion];
166+ [self _downloadAndWriteImageForURL: url key: key completionBlock: completion failureBlock: failure ];
142167 }
143168}
144169
145- - (void ) imageForURL : (NSURL *)url completionBlock : (void (^)(UIImage *image))completion {
146- [self imageForURL: url key: keyForURL (url) completionBlock: completion];
170+ - (void ) imageForURL : (NSURL *)url completionBlock : (void (^)(UIImage *image))completion failureBlock : ( void (^)( NSURLRequest *request, NSURLResponse *response, NSError * error)) failure {
171+ [self imageForURL: url key: keyForURL (url) completionBlock: completion failureBlock: (failure) ];
147172}
148173
149174- (UIImage *) cachedImageForKey : (NSString *)key {
@@ -185,7 +210,8 @@ - (UIImage *) imageForURL:(NSURL *)url key:(NSString*)key delegate:(id<JMImageCa
185210 [d cache: self didDownloadImage: image forURL: url key: key];
186211 }
187212 }
188- }];
213+ }
214+ failureBlock: nil ];
189215 }
190216
191217 return nil ;
0 commit comments