From eae76d26b79e26ab50b4ce9368b9946f99eec5d6 Mon Sep 17 00:00:00 2001 From: ricky cancro <@pinterest.com> Date: Thu, 30 Jul 2015 16:50:11 -0700 Subject: [PATCH] Add expectedSize to ASNetworkImageNode ASNetworkImageNode defers to ASImageNode to return its calculatedSize. ASImageNode returns the size of its image. There is a good chance that ASNetworkImageNode hasn't downloaded its image yet when calculatedSize is called, so it returns a size of CGSizeZero. On top of that, it is possible that the size of the image is not actually the size that we wish to display in our node. I've added an "expectedImageSize" property that can be used to determine the calculatedSize of an ASNetworkImageNode. --- AsyncDisplayKit/ASNetworkImageNode.h | 5 +++++ AsyncDisplayKit/ASNetworkImageNode.mm | 23 +++++++++++++++++++++++ examples/Kittens/Sample/KittenNode.mm | 5 ++--- 3 files changed, 30 insertions(+), 3 deletions(-) diff --git a/AsyncDisplayKit/ASNetworkImageNode.h b/AsyncDisplayKit/ASNetworkImageNode.h index 5dc723521e..880202f485 100644 --- a/AsyncDisplayKit/ASNetworkImageNode.h +++ b/AsyncDisplayKit/ASNetworkImageNode.h @@ -51,6 +51,11 @@ */ @property (atomic, strong, readwrite) UIImage *defaultImage; +/** + * The expected size of the image that will download + */ +@property (atomic, assign, readwrite) CGSize expectedImageSize; + /** * The URL of a new image to download and display. * diff --git a/AsyncDisplayKit/ASNetworkImageNode.mm b/AsyncDisplayKit/ASNetworkImageNode.mm index 658176eeee..c37a4953e1 100644 --- a/AsyncDisplayKit/ASNetworkImageNode.mm +++ b/AsyncDisplayKit/ASNetworkImageNode.mm @@ -24,6 +24,7 @@ NSURL *_URL; UIImage *_defaultImage; + CGSize _expectedImageSize; NSUUID *_cacheUUID; id _imageDownload; @@ -44,6 +45,7 @@ _cache = cache; _downloader = downloader; _shouldCacheImage = YES; + _expectedImageSize = CGSizeZero; return self; } @@ -111,6 +113,18 @@ return _defaultImage; } +-(void)setExpectedImageSize:(CGSize)expectedImageSize +{ + ASDN::MutexLocker l(_lock); + _expectedImageSize = expectedImageSize; +} + +- (CGSize)expectedImageSize +{ + ASDN::MutexLocker l(_lock); + return _expectedImageSize; +} + - (void)setDelegate:(id)delegate { ASDN::MutexLocker l(_lock); @@ -153,6 +167,15 @@ } } +- (CGSize)calculateSizeThatFits:(CGSize)constrainedSize +{ + ASDN::MutexLocker l(_lock); + if (CGSizeEqualToSize(CGSizeZero, _expectedImageSize)) + return [super calculateSizeThatFits:constrainedSize]; + else + return _expectedImageSize; +} + #pragma mark - Private methods -- only call with lock. - (void)_cancelImageDownload diff --git a/examples/Kittens/Sample/KittenNode.mm b/examples/Kittens/Sample/KittenNode.mm index 7e321712cc..678d3c4796 100644 --- a/examples/Kittens/Sample/KittenNode.mm +++ b/examples/Kittens/Sample/KittenNode.mm @@ -83,6 +83,7 @@ static const CGFloat kInnerPadding = 10.0f; _imageNode.URL = [NSURL URLWithString:[NSString stringWithFormat:@"http://placekitten.com/%zd/%zd", (NSInteger)roundl(_kittenSize.width), (NSInteger)roundl(_kittenSize.height)]]; + _imageNode.expectedImageSize = CGSizeMake(kImageSize, kImageSize); // _imageNode.contentMode = UIViewContentModeCenter; [self addSubnode:_imageNode]; @@ -131,8 +132,6 @@ static const CGFloat kInnerPadding = 10.0f; #if UseAutomaticLayout - (ASLayoutSpec *)layoutSpecThatFits:(ASSizeRange)constrainedSize { - ASRatioLayoutSpec *imagePlaceholder = [ASRatioLayoutSpec newWithRatio:1.0 child:_imageNode]; - imagePlaceholder.flexBasis = ASRelativeDimensionMakeWithPoints(kImageSize); _textNode.flexShrink = YES; @@ -145,7 +144,7 @@ static const CGFloat kInnerPadding = 10.0f; .direction = ASStackLayoutDirectionHorizontal, .spacing = kInnerPadding } - children:@[imagePlaceholder, _textNode]]]; + children:@[_imageNode, _textNode]]]; } // With box model, you don't need to override this method, unless you want to add custom logic.