From 6819816bd36b5acb99075a7853eae0ee71bd0bd4 Mon Sep 17 00:00:00 2001 From: Nadine Salter Date: Tue, 3 Feb 2015 08:14:24 -0800 Subject: [PATCH] Restore artificial delay in Multiplex demo. The change to ASBasicImageDownloader regressed the artificial network delay in the original demo code. Revert it. --- examples/Multiplex/Sample/ViewController.m | 51 +++++++++++++++++++++- 1 file changed, 49 insertions(+), 2 deletions(-) diff --git a/examples/Multiplex/Sample/ViewController.m b/examples/Multiplex/Sample/ViewController.m index 13c82f06..6bb8431d 100644 --- a/examples/Multiplex/Sample/ViewController.m +++ b/examples/Multiplex/Sample/ViewController.m @@ -14,7 +14,7 @@ #import -@interface ViewController () +@interface ViewController () { ASMultiplexImageNode *_imageNode; @@ -33,7 +33,8 @@ // multiplex image node! - _imageNode = [[ASMultiplexImageNode alloc] initWithCache:nil downloader:[[ASBasicImageDownloader alloc] init]]; + // NB: we're using a custom downloader with an artificial delay for this demo, but ASBasicImageDownloader works too! + _imageNode = [[ASMultiplexImageNode alloc] initWithCache:nil downloader:self]; _imageNode.dataSource = self; _imageNode.delegate = self; @@ -132,4 +133,50 @@ } } + +#pragma mark - +#pragma mark ASImageDownloaderProtocol. + +- (id)downloadImageWithURL:(NSURL *)URL + callbackQueue:(dispatch_queue_t)callbackQueue + downloadProgressBlock:(void (^)(CGFloat progress))downloadProgressBlock + completion:(void (^)(CGImageRef image, NSError *error))completion +{ + // if no callback queue is supplied, run on the main thread + if (callbackQueue == nil) { + callbackQueue = dispatch_get_main_queue(); + } + + // call completion blocks + void (^handler)(NSURLResponse *, NSData *, NSError *) = ^(NSURLResponse *response, NSData *data, NSError *connectionError) { + // add an artificial delay + usleep(1.0 * USEC_PER_SEC); + + // ASMultiplexImageNode callbacks + dispatch_async(callbackQueue, ^{ + if (downloadProgressBlock) { + downloadProgressBlock(1.0f); + } + + if (completion) { + completion([[UIImage imageWithData:data] CGImage], connectionError); + } + }); + }; + + // let NSURLConnection do the heavy lifting + NSURLRequest *request = [NSURLRequest requestWithURL:URL]; + [NSURLConnection sendAsynchronousRequest:request + queue:[[NSOperationQueue alloc] init] + completionHandler:handler]; + + // return nil, don't support cancellation + return nil; +} + +- (void)cancelImageDownloadForIdentifier:(id)downloadIdentifier +{ + // no-op, don't support cancellation +} + @end