Merge pull request #1085 from chrisze/master

[ASNetworkImageNode] Add error handler to ASNetworkImageNodeDelegate
This commit is contained in:
appleguy 2016-01-22 20:05:59 -08:00
commit cb5e5704e2
2 changed files with 17 additions and 4 deletions

View File

@ -95,6 +95,16 @@ NS_ASSUME_NONNULL_BEGIN
@optional @optional
/**
* Notification that the image node failed to download the image.
*
* @param imageNode The sender.
* @param error The error with details.
*
* @discussion Called on a background queue.
*/
- (void)imageNode:(ASNetworkImageNode *)imageNode didFailWithError:(NSError *)error;
/** /**
* Notification that the image node finished decoding an image. * Notification that the image node finished decoding an image.
* *

View File

@ -168,14 +168,14 @@
_cacheUUID = nil; _cacheUUID = nil;
} }
- (void)_downloadImageWithCompletion:(void (^)(CGImageRef))finished - (void)_downloadImageWithCompletion:(void (^)(CGImageRef, NSError*))finished
{ {
_imageDownload = [_downloader downloadImageWithURL:_URL _imageDownload = [_downloader downloadImageWithURL:_URL
callbackQueue:dispatch_get_main_queue() callbackQueue:dispatch_get_main_queue()
downloadProgressBlock:NULL downloadProgressBlock:NULL
completion:^(CGImageRef responseImage, NSError *error) { completion:^(CGImageRef responseImage, NSError *error) {
if (finished != NULL) { if (finished != NULL) {
finished(responseImage); finished(responseImage, error);
} }
}]; }];
} }
@ -210,7 +210,7 @@
} }
} else { } else {
__weak __typeof__(self) weakSelf = self; __weak __typeof__(self) weakSelf = self;
void (^finished)(CGImageRef) = ^(CGImageRef responseImage) { void (^finished)(CGImageRef, NSError *) = ^(CGImageRef responseImage, NSError *error) {
__typeof__(self) strongSelf = weakSelf; __typeof__(self) strongSelf = weakSelf;
if (strongSelf == nil) { if (strongSelf == nil) {
return; return;
@ -232,6 +232,9 @@
if (responseImage != NULL) { if (responseImage != NULL) {
[strongSelf->_delegate imageNode:strongSelf didLoadImage:strongSelf.image]; [strongSelf->_delegate imageNode:strongSelf didLoadImage:strongSelf.image];
} }
else if (error && [strongSelf->_delegate respondsToSelector:@selector(imageNode:didFailWithError:)]) {
[strongSelf->_delegate imageNode:strongSelf didFailWithError:error];
}
}; };
if (_cache != nil) { if (_cache != nil) {
@ -247,7 +250,7 @@
if (image == NULL && _downloader != nil) { if (image == NULL && _downloader != nil) {
[self _downloadImageWithCompletion:finished]; [self _downloadImageWithCompletion:finished];
} else { } else {
finished(image); finished(image, NULL);
} }
}; };