Skip to content

Commit

Permalink
Added failureBlock to allow proper handing of errors situations
Browse files Browse the repository at this point in the history
  • Loading branch information
Brendt committed Jan 7, 2013
1 parent a09d871 commit 1af09be
Show file tree
Hide file tree
Showing 4 changed files with 67 additions and 28 deletions.
4 changes: 2 additions & 2 deletions JMImageCache.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
76 changes: 51 additions & 25 deletions JMImageCache.m
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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);
});
}
});
}

Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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;
Expand Down
3 changes: 3 additions & 0 deletions UIImageView+JMImageCache.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
12 changes: 11 additions & 1 deletion UIImageView+JMImageCache.m
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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);
}];
}
});
Expand Down

0 comments on commit 1af09be

Please sign in to comment.