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.
This commit is contained in:
ricky cancro 2015-07-30 16:50:11 -07:00
parent e76fa2794f
commit eae76d26b7
3 changed files with 30 additions and 3 deletions

View File

@ -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.
*

View File

@ -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<ASNetworkImageNodeDelegate>)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

View File

@ -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.