Improve ASViewController example

This commit is contained in:
Michael Schneider
2016-03-16 09:36:35 -07:00
parent 192e9398e5
commit 788cdbd326
5 changed files with 23 additions and 51 deletions

View File

@@ -21,32 +21,33 @@
self = [super init];
if (self == nil) { return self; }
_imageNode = [ASNetworkImageNode new];
_imageNode = [[ASNetworkImageNode alloc] init];
_imageNode.backgroundColor = ASDisplayNodeDefaultPlaceholderColor();
[self addSubnode:_imageNode];
return self;
}
#pragma mark - ASDisplayNode
- (ASLayoutSpec *)layoutSpecThatFits:(ASSizeRange)constrainedSize
{
ASStaticLayoutSpec *staticSpec = [ASStaticLayoutSpec staticLayoutSpecWithChildren:@[self.imageNode]];
self.imageNode.position = CGPointZero;
self.imageNode.sizeRange = ASRelativeSizeRangeMakeWithExactCGSize(constrainedSize.max);
return staticSpec;
return [ASStaticLayoutSpec staticLayoutSpecWithChildren:@[self.imageNode]];
}
- (void)fetchData
- (void)layoutDidFinish
{
[super fetchData];
[super layoutDidFinish];
[self loadImage];
// In general set URL of ASNetworkImageNode as soon as possible. Ideally in init or a
// view model setter method.
// In this case as we need to know the size of the node the url is set in layoutDidFinish so
// we have the calculatedSize available
self.imageNode.URL = [self imageURL];
}
#pragma mark - Image
- (NSURL *)imageURL
@@ -56,9 +57,4 @@
return [NSURL URLWithString:imageURLString];
}
- (void)loadImage
{
self.imageNode.URL = self.imageURL;
}
@end