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.