Merge pull request #1225 from garrettmoon/addImageLoadingDelegateMethod

[ASNetworkImageNode] Add fetch data delegate method; cache respondsToSelector results.
This commit is contained in:
appleguy 2016-02-12 15:22:54 -08:00
commit f3696cfef0
2 changed files with 35 additions and 6 deletions

View File

@ -95,6 +95,15 @@ NS_ASSUME_NONNULL_BEGIN
@optional @optional
/**
* Notification that the image node started to load
*
* @param imageNode The sender.
*
* @discussion Called on a background queue.
*/
- (void)imageNodeDidStartFetchingData:(ASNetworkImageNode *)imageNode;
/** /**
* Notification that the image node failed to download the image. * Notification that the image node failed to download the image.
* *

View File

@ -35,10 +35,15 @@
BOOL _imageLoaded; BOOL _imageLoaded;
BOOL _delegateSupportsDidStartFetchingData;
BOOL _delegateSupportsDidFailWithError;
BOOL _delegateSupportsImageNodeDidFinishDecoding;
//set on init only //set on init only
BOOL _downloaderSupportsNewProtocol; BOOL _downloaderSupportsNewProtocol;
BOOL _downloaderImplementsSetProgress; BOOL _downloaderImplementsSetProgress;
BOOL _downloaderImplementsSetPriority; BOOL _downloaderImplementsSetPriority;
BOOL _cacheSupportsNewProtocol; BOOL _cacheSupportsNewProtocol;
BOOL _cacheSupportsClearing; BOOL _cacheSupportsClearing;
} }
@ -144,6 +149,10 @@
{ {
ASDN::MutexLocker l(_lock); ASDN::MutexLocker l(_lock);
_delegate = delegate; _delegate = delegate;
_delegateSupportsDidStartFetchingData = [delegate respondsToSelector:@selector(imageNodeDidStartFetchingData:)];
_delegateSupportsDidFailWithError = [delegate respondsToSelector:@selector(imageNode:didFailWithError:)];
_delegateSupportsImageNodeDidFinishDecoding = [delegate respondsToSelector:@selector(imageNodeDidFinishDecoding:)];
} }
- (id<ASNetworkImageNodeDelegate>)delegate - (id<ASNetworkImageNodeDelegate>)delegate
@ -278,6 +287,13 @@
- (void)_lazilyLoadImageIfNecessary - (void)_lazilyLoadImageIfNecessary
{ {
if (!_imageLoaded && _URL != nil && _downloadIdentifier == nil) { if (!_imageLoaded && _URL != nil && _downloadIdentifier == nil) {
{
ASDN::MutexLocker l(_lock);
if (_delegateSupportsDidStartFetchingData) {
[_delegate imageNodeDidStartFetchingData:self];
}
}
if (_URL.isFileURL) { if (_URL.isFileURL) {
{ {
ASDN::MutexLocker l(_lock); ASDN::MutexLocker l(_lock);
@ -329,11 +345,14 @@
strongSelf->_cacheUUID = nil; strongSelf->_cacheUUID = nil;
} }
if (responseImage != NULL) { {
[strongSelf->_delegate imageNode:strongSelf didLoadImage:strongSelf.image]; ASDN::MutexLocker l(strongSelf->_lock);
} if (responseImage != NULL) {
else if (error && [strongSelf->_delegate respondsToSelector:@selector(imageNode:didFailWithError:)]) { [strongSelf->_delegate imageNode:strongSelf didLoadImage:strongSelf.image];
[strongSelf->_delegate imageNode:strongSelf didFailWithError:error]; }
else if (error && _delegateSupportsDidFailWithError) {
[strongSelf->_delegate imageNode:strongSelf didFailWithError:error];
}
} }
}; };
@ -377,7 +396,8 @@
- (void)asyncdisplaykit_asyncTransactionContainerStateDidChange - (void)asyncdisplaykit_asyncTransactionContainerStateDidChange
{ {
if (self.asyncdisplaykit_asyncTransactionContainerState == ASAsyncTransactionContainerStateNoTransactions) { if (self.asyncdisplaykit_asyncTransactionContainerState == ASAsyncTransactionContainerStateNoTransactions) {
if (self.layer.contents != nil && [self.delegate respondsToSelector:@selector(imageNodeDidFinishDecoding:)]) { ASDN::MutexLocker l(_lock);
if (self.layer.contents != nil && _delegateSupportsImageNodeDidFinishDecoding) {
[self.delegate imageNodeDidFinishDecoding:self]; [self.delegate imageNodeDidFinishDecoding:self];
} }
} }