diff --git a/AsyncDisplayKit/ASMultiplexImageNode.mm b/AsyncDisplayKit/ASMultiplexImageNode.mm index 351ff9c7d9..8215ece244 100644 --- a/AsyncDisplayKit/ASMultiplexImageNode.mm +++ b/AsyncDisplayKit/ASMultiplexImageNode.mm @@ -22,6 +22,7 @@ #import "ASPhotosFrameworkImageRequest.h" #import "ASEqualityHelpers.h" #import "ASInternalHelpers.h" +#import "ASDisplayNodeExtras.h" #if !AS_IOS8_SDK_OR_LATER #error ASMultiplexImageNode can be used on iOS 7, but must be linked against the iOS 8 SDK. @@ -293,9 +294,8 @@ typedef void(^ASMultiplexImageLoadCompletionBlock)(UIImage *image, id imageIdent - (void)visibilityDidChange:(BOOL)isVisible { [super visibilityDidChange:isVisible]; - + ASDN::MutexLocker l(_downloadIdentifierLock); if (_downloaderImplementsSetPriority) { - ASDN::MutexLocker l(_downloadIdentifierLock); if (_downloadIdentifier != nil) { if (isVisible) { [_downloader setPriority:ASImageDownloaderPriorityVisible withDownloadIdentifier:_downloadIdentifier]; @@ -305,32 +305,7 @@ typedef void(^ASMultiplexImageLoadCompletionBlock)(UIImage *image, id imageIdent } } - if (_downloaderImplementsSetProgress) { - ASDN::MutexLocker l(_downloadIdentifierLock); - - if (_downloadIdentifier != nil) { - __weak __typeof__(self) weakSelf = self; - ASImageDownloaderProgressImage progress = nil; - if (isVisible) { - progress = ^(UIImage * _Nonnull progressImage, id _Nullable downloadIdentifier) { - __typeof__(self) strongSelf = weakSelf; - if (strongSelf == nil) { - return; - } - - ASDN::MutexLocker l(strongSelf->_downloadIdentifierLock); - //Getting a result back for a different download identifier, download must not have been successfully canceled - if (ASObjectIsEqual(strongSelf->_downloadIdentifier, downloadIdentifier) == NO && downloadIdentifier != nil) { - return; - } - - strongSelf.image = progressImage; - }; - } - - [_downloader setProgressImageBlock:progress callbackQueue:dispatch_get_main_queue() withDownloadIdentifier:_downloadIdentifier]; - } - } + [self _updateProgressImageBlockOnDownloaderIfNeeded]; } #pragma mark - Core @@ -478,6 +453,33 @@ typedef void(^ASMultiplexImageLoadCompletionBlock)(UIImage *image, id imageIdent } #pragma mark - + +- (void)_updateProgressImageBlockOnDownloaderIfNeeded +{ + if (!_downloaderImplementsSetProgress || _downloadIdentifier == nil) { + return; + } + + ASImageDownloaderProgressImage progress = nil; + if (ASInterfaceStateIncludesVisible(self.interfaceState)) { + __weak __typeof__(self) weakSelf = self; + progress = ^(UIImage * _Nonnull progressImage, id _Nullable downloadIdentifier) { + __typeof__(self) strongSelf = weakSelf; + if (strongSelf == nil) { + return; + } + + ASDN::MutexLocker l(strongSelf->_downloadIdentifierLock); + //Getting a result back for a different download identifier, download must not have been successfully canceled + if (ASObjectIsEqual(strongSelf->_downloadIdentifier, downloadIdentifier) == NO && downloadIdentifier != nil) { + return; + } + strongSelf.image = progressImage; + }; + } + [_downloader setProgressImageBlock:progress callbackQueue:dispatch_get_main_queue() withDownloadIdentifier:_downloadIdentifier]; +} + - (void)_clearImage { // Destruction of bigger images on the main thread can be expensive @@ -823,6 +825,7 @@ typedef void(^ASMultiplexImageLoadCompletionBlock)(UIImage *image, id imageIdent }]]; #pragma clang diagnostic pop } + [self _updateProgressImageBlockOnDownloaderIfNeeded]; }); } diff --git a/AsyncDisplayKit/ASNetworkImageNode.mm b/AsyncDisplayKit/ASNetworkImageNode.mm index 258575f0d1..fb3df668e1 100755 --- a/AsyncDisplayKit/ASNetworkImageNode.mm +++ b/AsyncDisplayKit/ASNetworkImageNode.mm @@ -212,6 +212,7 @@ static const CGSize kMinReleaseImageOnBackgroundSize = {20.0, 20.0}; - (void)visibilityDidChange:(BOOL)isVisible { [super visibilityDidChange:isVisible]; + ASDN::MutexLocker l(_lock); if (_downloaderImplementsSetPriority) { if (_downloadIdentifier != nil) { @@ -388,7 +389,7 @@ static const CGSize kMinReleaseImageOnBackgroundSize = {20.0, 20.0}; ASDN::MutexLocker l(strongSelf->_lock); //Getting a result back for a different download identifier, download must not have been successfully canceled - if (downloadIdentifier != nil && !ASObjectIsEqual(strongSelf->_downloadIdentifier, downloadIdentifier)) { + if (ASObjectIsEqual(strongSelf->_downloadIdentifier, downloadIdentifier) == NO && downloadIdentifier != nil) { return; }