1414#import < AsyncDisplayKit/AsyncDisplayKit.h>
1515
1616
17- @interface ViewController () <ASMultiplexImageNodeDataSource, ASMultiplexImageNodeDelegate>
17+ @interface ViewController () <ASMultiplexImageNodeDataSource, ASMultiplexImageNodeDelegate, ASImageDownloaderProtocol >
1818{
1919 ASMultiplexImageNode *_imageNode;
2020
@@ -33,7 +33,8 @@ - (instancetype)init
3333
3434
3535 // multiplex image node!
36- _imageNode = [[ASMultiplexImageNode alloc ] initWithCache: nil downloader: [[ASBasicImageDownloader alloc ] init ]];
36+ // NB: we're using a custom downloader with an artificial delay for this demo, but ASBasicImageDownloader works too!
37+ _imageNode = [[ASMultiplexImageNode alloc ] initWithCache: nil downloader: self ];
3738 _imageNode.dataSource = self;
3839 _imageNode.delegate = self;
3940
@@ -132,4 +133,50 @@ - (void)multiplexImageNode:(ASMultiplexImageNode *)imageNode didFinishDownloadin
132133 }
133134}
134135
136+
137+ #pragma mark -
138+ #pragma mark ASImageDownloaderProtocol.
139+
140+ - (id )downloadImageWithURL : (NSURL *)URL
141+ callbackQueue : (dispatch_queue_t )callbackQueue
142+ downloadProgressBlock : (void (^)(CGFloat progress))downloadProgressBlock
143+ completion : (void (^)(CGImageRef image, NSError *error))completion
144+ {
145+ // if no callback queue is supplied, run on the main thread
146+ if (callbackQueue == nil ) {
147+ callbackQueue = dispatch_get_main_queue ();
148+ }
149+
150+ // call completion blocks
151+ void (^handler)(NSURLResponse *, NSData *, NSError *) = ^(NSURLResponse *response, NSData *data, NSError *connectionError) {
152+ // add an artificial delay
153+ usleep (1.0 * USEC_PER_SEC);
154+
155+ // ASMultiplexImageNode callbacks
156+ dispatch_async (callbackQueue, ^{
157+ if (downloadProgressBlock) {
158+ downloadProgressBlock (1 .0f );
159+ }
160+
161+ if (completion) {
162+ completion ([[UIImage imageWithData: data] CGImage ], connectionError);
163+ }
164+ });
165+ };
166+
167+ // let NSURLConnection do the heavy lifting
168+ NSURLRequest *request = [NSURLRequest requestWithURL: URL];
169+ [NSURLConnection sendAsynchronousRequest: request
170+ queue: [[NSOperationQueue alloc ] init ]
171+ completionHandler: handler];
172+
173+ // return nil, don't support cancellation
174+ return nil ;
175+ }
176+
177+ - (void )cancelImageDownloadForIdentifier : (id )downloadIdentifier
178+ {
179+ // no-op, don't support cancellation
180+ }
181+
135182@end
0 commit comments