|
| 1 | +// |
| 2 | +// ScreenNode.m |
| 3 | +// Sample |
| 4 | +// |
| 5 | +// Created by Huy Nguyen on 16/09/15. |
| 6 | +// Copyright (c) 2015 Facebook. All rights reserved. |
| 7 | +// |
| 8 | + |
| 9 | +#import "ScreenNode.h" |
| 10 | + |
| 11 | +@interface ScreenNode() <ASMultiplexImageNodeDataSource, ASMultiplexImageNodeDelegate, ASImageDownloaderProtocol> |
| 12 | +@end |
| 13 | + |
| 14 | +@implementation ScreenNode |
| 15 | + |
| 16 | +- (instancetype)init |
| 17 | +{ |
| 18 | + if (!(self = [super init])) { |
| 19 | + return nil; |
| 20 | + } |
| 21 | + |
| 22 | + // multiplex image node! |
| 23 | + // NB: we're using a custom downloader with an artificial delay for this demo, but ASBasicImageDownloader works too! |
| 24 | + _imageNode = [[ASMultiplexImageNode alloc] initWithCache:nil downloader:self]; |
| 25 | + _imageNode.dataSource = self; |
| 26 | + _imageNode.delegate = self; |
| 27 | + |
| 28 | + // placeholder colour |
| 29 | + _imageNode.backgroundColor = ASDisplayNodeDefaultPlaceholderColor(); |
| 30 | + |
| 31 | + // load low-quality images before high-quality images |
| 32 | + _imageNode.downloadsIntermediateImages = YES; |
| 33 | + |
| 34 | + // simple status label |
| 35 | + _textNode = [[ASTextNode alloc] init]; |
| 36 | + |
| 37 | + [self addSubnode:_imageNode]; |
| 38 | + [self addSubnode:_textNode]; |
| 39 | + |
| 40 | + return self; |
| 41 | +} |
| 42 | + |
| 43 | +- (void)start |
| 44 | +{ |
| 45 | + [self setText:@"loading…"]; |
| 46 | + _textNode.userInteractionEnabled = NO; |
| 47 | + _imageNode.imageIdentifiers = @[ @"best", @"medium", @"worst" ]; // go! |
| 48 | +} |
| 49 | + |
| 50 | +- (void)reload { |
| 51 | + [self start]; |
| 52 | + [_imageNode reloadImageIdentifierSources]; |
| 53 | +} |
| 54 | + |
| 55 | +- (void)setText:(NSString *)text |
| 56 | +{ |
| 57 | + NSDictionary *attributes = @{NSFontAttributeName: [UIFont fontWithName:@"HelveticaNeue-Light" size:22.0f]}; |
| 58 | + NSAttributedString *string = [[NSAttributedString alloc] initWithString:text |
| 59 | + attributes:attributes]; |
| 60 | + _textNode.attributedString = string; |
| 61 | + [self setNeedsLayout]; |
| 62 | +} |
| 63 | + |
| 64 | +- (ASLayoutSpec *)layoutSpecThatFits:(ASSizeRange)constrainedSize |
| 65 | +{ |
| 66 | + ASRatioLayoutSpec *imagePlaceholder = [ASRatioLayoutSpec ratioLayoutSpecWithRatio:1 child:_imageNode]; |
| 67 | + ASStackLayoutSpec *verticalStack = [ASStackLayoutSpec stackLayoutSpecWithDirection:ASStackLayoutDirectionVertical |
| 68 | + spacing:10 |
| 69 | + justifyContent:ASStackLayoutJustifyContentCenter |
| 70 | + alignItems:ASStackLayoutAlignItemsCenter |
| 71 | + children:@[imagePlaceholder, _textNode]]; |
| 72 | + return [ASInsetLayoutSpec insetLayoutSpecWithInsets:UIEdgeInsetsMake(10, 10, 10, 10) child:verticalStack]; |
| 73 | +} |
| 74 | + |
| 75 | +#pragma mark - |
| 76 | +#pragma mark ASMultiplexImageNode data source & delegate. |
| 77 | + |
| 78 | +- (NSURL *)multiplexImageNode:(ASMultiplexImageNode *)imageNode URLForImageIdentifier:(id)imageIdentifier |
| 79 | +{ |
| 80 | + if ([imageIdentifier isEqualToString:@"worst"]) { |
| 81 | + return [NSURL URLWithString:@"https://raw.githubusercontent.com/facebook/AsyncDisplayKit/master/examples/Multiplex/worst.png"]; |
| 82 | + } |
| 83 | + |
| 84 | + if ([imageIdentifier isEqualToString:@"medium"]) { |
| 85 | + return [NSURL URLWithString:@"https://raw.githubusercontent.com/facebook/AsyncDisplayKit/master/examples/Multiplex/medium.png"]; |
| 86 | + } |
| 87 | + |
| 88 | + if ([imageIdentifier isEqualToString:@"best"]) { |
| 89 | + return [NSURL URLWithString:@"https://raw.githubusercontent.com/facebook/AsyncDisplayKit/master/examples/Multiplex/best.png"]; |
| 90 | + } |
| 91 | + |
| 92 | + // unexpected identifier |
| 93 | + return nil; |
| 94 | +} |
| 95 | + |
| 96 | +- (void)multiplexImageNode:(ASMultiplexImageNode *)imageNode didFinishDownloadingImageWithIdentifier:(id)imageIdentifier error:(NSError *)error |
| 97 | +{ |
| 98 | + [self setText:[NSString stringWithFormat:@"loaded '%@'", imageIdentifier]]; |
| 99 | + |
| 100 | + if ([imageIdentifier isEqualToString:@"best"]) { |
| 101 | + [self setText:[_textNode.attributedString.string stringByAppendingString:@". tap to reload"]]; |
| 102 | + _textNode.userInteractionEnabled = YES; |
| 103 | + } |
| 104 | +} |
| 105 | + |
| 106 | + |
| 107 | +#pragma mark - |
| 108 | +#pragma mark ASImageDownloaderProtocol. |
| 109 | + |
| 110 | +- (id)downloadImageWithURL:(NSURL *)URL |
| 111 | + callbackQueue:(dispatch_queue_t)callbackQueue |
| 112 | + downloadProgressBlock:(void (^)(CGFloat progress))downloadProgressBlock |
| 113 | + completion:(void (^)(CGImageRef image, NSError *error))completion |
| 114 | +{ |
| 115 | + // if no callback queue is supplied, run on the main thread |
| 116 | + if (callbackQueue == nil) { |
| 117 | + callbackQueue = dispatch_get_main_queue(); |
| 118 | + } |
| 119 | + |
| 120 | + // call completion blocks |
| 121 | + void (^handler)(NSURLResponse *, NSData *, NSError *) = ^(NSURLResponse *response, NSData *data, NSError *connectionError) { |
| 122 | + // add an artificial delay |
| 123 | + usleep(1.0 * USEC_PER_SEC); |
| 124 | + |
| 125 | + // ASMultiplexImageNode callbacks |
| 126 | + dispatch_async(callbackQueue, ^{ |
| 127 | + if (downloadProgressBlock) { |
| 128 | + downloadProgressBlock(1.0f); |
| 129 | + } |
| 130 | + |
| 131 | + if (completion) { |
| 132 | + completion([[UIImage imageWithData:data] CGImage], connectionError); |
| 133 | + } |
| 134 | + }); |
| 135 | + }; |
| 136 | + |
| 137 | + // let NSURLConnection do the heavy lifting |
| 138 | + NSURLRequest *request = [NSURLRequest requestWithURL:URL]; |
| 139 | + [NSURLConnection sendAsynchronousRequest:request |
| 140 | + queue:[[NSOperationQueue alloc] init] |
| 141 | + completionHandler:handler]; |
| 142 | + |
| 143 | + // return nil, don't support cancellation |
| 144 | + return nil; |
| 145 | +} |
| 146 | + |
| 147 | +- (void)cancelImageDownloadForIdentifier:(id)downloadIdentifier |
| 148 | +{ |
| 149 | + // no-op, don't support cancellation |
| 150 | +} |
| 151 | + |
| 152 | +@end |
0 commit comments