From 2bc701d23f5ab99a6610e9602b6101aa67504272 Mon Sep 17 00:00:00 2001 From: David Robles Date: Thu, 5 Jan 2017 14:59:11 -0800 Subject: [PATCH] =?UTF-8?q?Copy=20ASTextNode=E2=80=99s=20implementation=20?= =?UTF-8?q?of=20placeholderImage=20into=20ASImageNode=20in=20order=20to=20?= =?UTF-8?q?fix=20the=20usage=20of=20placeholderColor.=20(#2866)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- AsyncDisplayKit/ASImageNode.mm | 21 +++++++++++++++++++++ examples/Kittens/Sample/KittenNode.mm | 3 ++- 2 files changed, 23 insertions(+), 1 deletion(-) diff --git a/AsyncDisplayKit/ASImageNode.mm b/AsyncDisplayKit/ASImageNode.mm index e0ee593933..bbe8130ef1 100644 --- a/AsyncDisplayKit/ASImageNode.mm +++ b/AsyncDisplayKit/ASImageNode.mm @@ -27,6 +27,7 @@ #import "ASEqualityHelpers.h" #import "ASEqualityHashHelpers.h" #import "ASWeakMap.h" +#import "CoreGraphics+ASConvenience.h" // TODO: It would be nice to remove this dependency; it's the only subclass using more than +FrameworkSubclasses.h #import "ASDisplayNodeInternal.h" @@ -183,6 +184,26 @@ struct ASImageNodeDrawParameters { [self invalidateAnimatedImage]; } +- (UIImage *)placeholderImage +{ + // FIXME: Replace this implementation with reusable CALayers that have .backgroundColor set. + // This would completely eliminate the memory and performance cost of the backing store. + CGSize size = self.calculatedSize; + if ((size.width * size.height) < CGFLOAT_EPSILON) { + return nil; + } + + ASDN::MutexLocker l(__instanceLock__); + + UIGraphicsBeginImageContext(size); + [self.placeholderColor setFill]; + UIRectFill(CGRectMake(0, 0, size.width, size.height)); + UIImage *image = UIGraphicsGetImageFromCurrentImageContext(); + UIGraphicsEndImageContext(); + + return image; +} + #pragma mark - Layout and Sizing - (CGSize)calculateSizeThatFits:(CGSize)constrainedSize diff --git a/examples/Kittens/Sample/KittenNode.mm b/examples/Kittens/Sample/KittenNode.mm index 67e586b4a7..db215cb40b 100644 --- a/examples/Kittens/Sample/KittenNode.mm +++ b/examples/Kittens/Sample/KittenNode.mm @@ -88,10 +88,11 @@ static const CGFloat kInnerPadding = 10.0f; // kitten image, with a solid background colour serving as placeholder _imageNode = [[ASNetworkImageNode alloc] init]; - _imageNode.backgroundColor = ASDisplayNodeDefaultPlaceholderColor(); _imageNode.URL = [NSURL URLWithString:[NSString stringWithFormat:@"https://placekitten.com/%zd/%zd", (NSInteger)roundl(_kittenSize.width), (NSInteger)roundl(_kittenSize.height)]]; + _imageNode.placeholderFadeDuration = .5; + _imageNode.placeholderColor = ASDisplayNodeDefaultPlaceholderColor(); // _imageNode.contentMode = UIViewContentModeCenter; [_imageNode addTarget:self action:@selector(toggleNodesSwap) forControlEvents:ASControlNodeEventTouchUpInside]; [self addSubnode:_imageNode];