Skip to content

Loading…

failureBlock #17

Merged
merged 1 commit into from

2 participants

@Brendtos

Hi Jake,

Nice work on this library. It was just what I was looking for.

I've extended it a bit to allow for handing for errors though a failureBlock. The project I'm working on requires an authenticated session which can now be handled in the failureBlock and the requested attempted again. Thought others might find this useful also.

Thanks again for making this library available.

Cheers,
Brendt

@jakemarsh jakemarsh merged commit f3063f2 into jakemarsh:master
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Jan 7, 2013
Showing with 67 additions and 28 deletions.
  1. +2 −2 JMImageCache.h
  2. +51 −25 JMImageCache.m
  3. +3 −0 UIImageView+JMImageCache.h
  4. +11 −1 UIImageView+JMImageCache.m
View
4 JMImageCache.h
@@ -22,8 +22,8 @@
+ (JMImageCache *) sharedCache;
-- (void) imageForURL:(NSURL *)url key:(NSString *)key completionBlock:(void (^)(UIImage *image))completion;
-- (void) imageForURL:(NSURL *)url completionBlock:(void (^)(UIImage *image))completion;
+- (void) imageForURL:(NSURL *)url key:(NSString *)key completionBlock:(void (^)(UIImage *image))completion failureBlock:(void (^)(NSURLRequest *request, NSURLResponse *response, NSError* error))failure;
+- (void) imageForURL:(NSURL *)url completionBlock:(void (^)(UIImage *image))completion failureBlock:(void (^)(NSURLRequest *request, NSURLResponse *response, NSError* error))failure;
- (UIImage *) cachedImageForKey:(NSString *)key;
- (UIImage *) cachedImageForURL:(NSURL *)url;
View
76 JMImageCache.m
@@ -31,7 +31,7 @@ @interface JMImageCache ()
@property (strong, nonatomic) NSOperationQueue *diskOperationQueue;
-- (void) _downloadAndWriteImageForURL:(NSURL *)url key:(NSString *)key completionBlock:(void (^)(UIImage *image))completion;
+- (void) _downloadAndWriteImageForURL:(NSURL *)url key:(NSString *)key completionBlock:(void (^)(UIImage *image))completion failureBlock:(void (^)(NSURLRequest *request, NSURLResponse *response, NSError* error))failure;
@end
@@ -60,7 +60,8 @@ - (id) init {
return self;
}
-- (void) _downloadAndWriteImageForURL:(NSURL *)url key:(NSString *)key completionBlock:(void (^)(UIImage *image))completion {
+- (void) _downloadAndWriteImageForURL:(NSURL *)url key:(NSString *)key completionBlock:(void (^)(UIImage *image))completion failureBlock:(void (^)(NSURLRequest *request, NSURLResponse *response, NSError* error))failure
+{
if (!key && !url) return;
if (!key) {
@@ -68,25 +69,49 @@ - (void) _downloadAndWriteImageForURL:(NSURL *)url key:(NSString *)key completio
}
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
- NSData *data = [NSData dataWithContentsOfURL:url];
- UIImage *i = [[UIImage alloc] initWithData:data];
- // stop process if the method could not initialize the image from the specified data
- if (!i) return;
- NSString *cachePath = cachePathForKey(key);
- 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 forKey:key];
-
- dispatch_async(dispatch_get_main_queue(), ^{
- if(completion) completion(i);
- });
+ NSURLRequest* request = [NSURLRequest requestWithURL:url];
+ NSURLResponse* response = nil;
+ NSError* error = nil;
+ NSData* data = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];
+
+ if (error)
+ {
+ dispatch_async(dispatch_get_main_queue(), ^{
+
+ if(failure) failure(request, response, error);
+ });
+ return;
+ }
+
+ UIImage *i = [[UIImage alloc] initWithData:data];
+ if (!i)
+ {
+ NSMutableDictionary *errorDetail = [NSMutableDictionary dictionary];
+ [errorDetail setValue:[NSString stringWithFormat:@"Failed to init image with data from for URL: %@", url] forKey:NSLocalizedDescriptionKey];
+ NSError* error = [NSError errorWithDomain:@"JMImageCacheErrorDomain" code:1 userInfo:errorDetail];
+ dispatch_async(dispatch_get_main_queue(), ^{
+
+ if(failure) failure(request, response, error);
+ });
+ }
+ else
+ {
+ NSString *cachePath = cachePathForKey(key);
+ 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 forKey:key];
+
+ dispatch_async(dispatch_get_main_queue(), ^{
+ if(completion) completion(i);
+ });
+ }
});
}
@@ -131,19 +156,19 @@ - (void) removeObjectForKey:(id)key {
#pragma mark -
#pragma mark Getter Methods
-- (void) imageForURL:(NSURL *)url key:(NSString *)key completionBlock:(void (^)(UIImage *image))completion {
+- (void) imageForURL:(NSURL *)url key:(NSString *)key completionBlock:(void (^)(UIImage *image))completion failureBlock:(void (^)(NSURLRequest *request, NSURLResponse *response, NSError* error))failure{
UIImage *i = [self cachedImageForKey:key];
if(i) {
if(completion) completion(i);
} else {
- [self _downloadAndWriteImageForURL:url key:key completionBlock:completion];
+ [self _downloadAndWriteImageForURL:url key:key completionBlock:completion failureBlock:failure];
}
}
-- (void) imageForURL:(NSURL *)url completionBlock:(void (^)(UIImage *image))completion {
- [self imageForURL:url key:keyForURL(url) completionBlock:completion];
+- (void) imageForURL:(NSURL *)url completionBlock:(void (^)(UIImage *image))completion failureBlock:(void (^)(NSURLRequest *request, NSURLResponse *response, NSError* error))failure{
+ [self imageForURL:url key:keyForURL(url) completionBlock:completion failureBlock:(failure)];
}
- (UIImage *) cachedImageForKey:(NSString *)key {
@@ -185,7 +210,8 @@ - (UIImage *) imageForURL:(NSURL *)url key:(NSString*)key delegate:(id<JMImageCa
[d cache:self didDownloadImage:image forURL:url key:key];
}
}
- }];
+ }
+ failureBlock:nil];
}
return nil;
View
3 UIImageView+JMImageCache.h
@@ -13,7 +13,10 @@
- (void) setImageWithURL:(NSURL *)url;
- (void) setImageWithURL:(NSURL *)url placeholder:(UIImage *)placeholderImage;
- (void) setImageWithURL:(NSURL *)url placeholder:(UIImage *)placeholderImage completionBlock:(void (^)(UIImage *))completionBlock;
+- (void) setImageWithURL:(NSURL *)url placeholder:(UIImage *)placeholderImage completionBlock:(void (^)(UIImage *))completionBlock failureBlock:(void (^)(NSURLRequest *request, NSURLResponse *response, NSError* error))failure;
- (void) setImageWithURL:(NSURL *)url key:(NSString*)key placeholder:(UIImage *)placeholderImage;
- (void) setImageWithURL:(NSURL *)url key:(NSString*)key placeholder:(UIImage *)placeholderImage completionBlock:(void (^)(UIImage *image))completionBlock;
+- (void) setImageWithURL:(NSURL *)url key:(NSString*)key placeholder:(UIImage *)placeholderImage completionBlock:(void (^)(UIImage *image))completionBlock failureBlock:(void (^)(NSURLRequest *request, NSURLResponse *response, NSError* error))failure;
+
@end
View
12 UIImageView+JMImageCache.m
@@ -44,12 +44,18 @@ - (void) setImageWithURL:(NSURL *)url placeholder:(UIImage *)placeholderImage {
[self setImageWithURL:url key:nil placeholder:placeholderImage];
}
- (void) setImageWithURL:(NSURL *)url placeholder:(UIImage *)placeholderImage completionBlock:(void (^)(UIImage *))completionBlock {
- [self setImageWithURL:url key:nil placeholder:placeholderImage completionBlock:completionBlock];
+ [self setImageWithURL:url key:nil placeholder:placeholderImage completionBlock:completionBlock failureBlock:nil];
+}
+- (void) setImageWithURL:(NSURL *)url placeholder:(UIImage *)placeholderImage completionBlock:(void (^)(UIImage *))completionBlock failureBlock:(void (^)(NSURLRequest *request, NSURLResponse *response, NSError* error))failureBlock {
+ [self setImageWithURL:url key:nil placeholder:placeholderImage completionBlock:completionBlock failureBlock:failureBlock];
}
- (void) setImageWithURL:(NSURL *)url key:(NSString*)key placeholder:(UIImage *)placeholderImage {
[self setImageWithURL:url key:key placeholder:placeholderImage completionBlock:nil];
}
- (void) setImageWithURL:(NSURL *)url key:(NSString*)key placeholder:(UIImage *)placeholderImage completionBlock:(void (^)(UIImage *image))completionBlock {
+ [self setImageWithURL:url key:key placeholder:placeholderImage completionBlock:completionBlock];
+}
+- (void) setImageWithURL:(NSURL *)url key:(NSString*)key placeholder:(UIImage *)placeholderImage completionBlock:(void (^)(UIImage *image))completionBlock failureBlock:(void (^)(NSURLRequest *request, NSURLResponse *response, NSError* error))failureBlock{
self.jm_imageURL = url;
self.image = placeholderImage;
@@ -101,6 +107,10 @@ - (void) setImageWithURL:(NSURL *)url key:(NSString*)key placeholder:(UIImage *)
if (completionBlock) completionBlock(image);
});
}
+ }
+ failureBlock:^(NSURLRequest *request, NSURLResponse *response, NSError* error)
+ {
+ if (failureBlock) failureBlock(request, response, error);
}];
}
});
Something went wrong with that request. Please try again.