Permalink
Browse files

Added failureBlock to allow proper handing of errors situations

  • Loading branch information...
1 parent a09d871 commit 1af09be78a04a875ff25bff6d3a323c1e719e22b Brendt committed 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
@@ -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
@@ -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,33 +60,58 @@ - (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) {
key = keyForURL(url);
}
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;
@@ -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
@@ -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);
}];
}
});

0 comments on commit 1af09be

Please sign in to comment.