Skip to content

Commit

Permalink
Improve ASNetworkImageNode delegate callout behavior (TextureGroup#778)
Browse files Browse the repository at this point in the history
* Improve ASNetworkImageNode delegate callout behavior

* no message
  • Loading branch information
Adlai-Holler authored and bernieperez committed Apr 25, 2018
1 parent 8ae09fd commit 30b2b80
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 25 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
- Modified `ASImageDownloaderCompletion` to add an optional `id userInfo` field. Your custom downloader can pass `nil`.
- Modified the last argument to `-[ASNetworkImageNodeDelegate imageNode:didLoadImage:info:]` method from a struct to an object of new class `ASNetworkImageLoadInfo` which includes other metadata about the load operation.
- Removed +load static initializer from ASDisplayNode. [Adlai Holler](https://github.com/Adlai-Holler)
- Optimized ASNetworkImageNode loading and resolved edge cases where the image provided to the delegate was not the image that was loaded. [Adlai Holler](https://github.com/Adlai-Holler) [#778](https://github.com/TextureGroup/Texture/pull/778/)

## 2.6
- [Xcode 9] Updated to require Xcode 9 (to fix warnings) [Garrett Moon](https://github.com/garrettmoon)
Expand Down
52 changes: 27 additions & 25 deletions Source/ASNetworkImageNode.mm
Original file line number Diff line number Diff line change
Expand Up @@ -703,47 +703,49 @@ - (void)_lazilyLoadImageIfNecessary
return;
}

UIImage *newImage;
if (imageContainer != nil) {
[strongSelf _locked_setCurrentImageQuality:1.0];
NSData *animatedImageData = [imageContainer asdk_animatedImageData];
if (animatedImageData && strongSelf->_downloaderFlags.downloaderImplementsAnimatedImage) {
id animatedImage = [strongSelf->_downloader animatedImageWithData:animatedImageData];
[strongSelf _locked_setAnimatedImage:animatedImage];
} else {
[strongSelf _locked__setImage:[imageContainer asdk_image]];
newImage = [imageContainer asdk_image];
[strongSelf _locked__setImage:newImage];
}
strongSelf->_imageLoaded = YES;
}

strongSelf->_downloadIdentifier = nil;
strongSelf->_cacheUUID = nil;

// TODO: Why dispatch to main here?
// The docs say the image node delegate methods
// are called from background.
ASPerformBlockOnMainThread(^{
__typeof__(self) strongSelf = weakSelf;
if (strongSelf == nil) {
return;
}

// Grab the lock for the rest of the block
ASDN::MutexLocker l(strongSelf->__instanceLock__);

if (imageContainer != nil) {
if (strongSelf->_delegateFlags.delegateDidLoadImageWithInfo) {
ASDN::MutexUnlocker u(strongSelf->__instanceLock__);
void (^calloutBlock)(ASNetworkImageNode *inst);

if (newImage) {
if (_delegateFlags.delegateDidLoadImageWithInfo) {
calloutBlock = ^(ASNetworkImageNode *strongSelf) {
auto info = [[ASNetworkImageLoadInfo alloc] initWithURL:URL sourceType:imageSource downloadIdentifier:downloadIdentifier userInfo:userInfo];
[delegate imageNode:strongSelf didLoadImage:strongSelf.image info:info];
} else if (strongSelf->_delegateFlags.delegateDidLoadImage) {
ASDN::MutexUnlocker u(strongSelf->__instanceLock__);
[delegate imageNode:strongSelf didLoadImage:strongSelf.image];
}
} else if (error && strongSelf->_delegateFlags.delegateDidFailWithError) {
ASDN::MutexUnlocker u(strongSelf->__instanceLock__);
[delegate imageNode:strongSelf didFailWithError:error];
[delegate imageNode:strongSelf didLoadImage:newImage info:info];
};
} else if (_delegateFlags.delegateDidLoadImage) {
calloutBlock = ^(ASNetworkImageNode *strongSelf) {
[delegate imageNode:strongSelf didLoadImage:newImage];
};
}
});
} else if (error && _delegateFlags.delegateDidFailWithError) {
calloutBlock = ^(ASNetworkImageNode *strongSelf) {
[delegate imageNode:strongSelf didFailWithError:error];
};
}

if (calloutBlock) {
ASPerformBlockOnMainThread(^{
if (auto strongSelf = weakSelf) {
calloutBlock(strongSelf);
}
});
}
});
};

Expand Down

0 comments on commit 30b2b80

Please sign in to comment.