From 6faa7bfcb5b73a31920ae7addf852673401f74f4 Mon Sep 17 00:00:00 2001 From: Giuseppe Lanza Date: Tue, 30 Jul 2013 17:14:52 +0200 Subject: [PATCH] added multiple download of images for animationImages property of UIImageView --- SDWebImage/UIImageView+WebCache.h | 9 +++++ SDWebImage/UIImageView+WebCache.m | 55 +++++++++++++++++++++++++++++++ 2 files changed, 64 insertions(+) diff --git a/SDWebImage/UIImageView+WebCache.h b/SDWebImage/UIImageView+WebCache.h index 333ae3e..952a1e9 100644 --- a/SDWebImage/UIImageView+WebCache.h +++ b/SDWebImage/UIImageView+WebCache.h @@ -134,9 +134,18 @@ */ - (void)setImageWithURL:(NSURL *)url placeholderImage:(UIImage *)placeholder options:(SDWebImageOptions)options progress:(SDWebImageDownloaderProgressBlock)progressBlock completed:(SDWebImageCompletedBlock)completedBlock; +/** + * Download an array of images and starts them in an animation loop + * + *@param arrayOfURLs An array of NSURL + */ +-(void)setAnimationImagesWithURLs:(NSArray *)arrayOfURLs; + /** * Cancel the current download */ - (void)cancelCurrentImageLoad; +- (void)cancelCurrentArrayLoad; + @end diff --git a/SDWebImage/UIImageView+WebCache.m b/SDWebImage/UIImageView+WebCache.m index 59e61a1..dd9bfcb 100644 --- a/SDWebImage/UIImageView+WebCache.m +++ b/SDWebImage/UIImageView+WebCache.m @@ -10,6 +10,7 @@ #import "objc/runtime.h" static char operationKey; +static char operationArrayKey; @implementation UIImageView (WebCache) @@ -82,6 +83,46 @@ static char operationKey; } } +-(void)setAnimationImagesWithURLs:(NSArray *)arrayOfURLs{ + [self cancelCurrentArrayLoad]; + __weak UIImageView *wself = self; + + NSMutableArray *operationsArray = [[NSMutableArray alloc] init]; + + for(NSURL *logoImageURL in arrayOfURLs){ + id operation = [SDWebImageManager.sharedManager downloadWithURL:logoImageURL options:0 progress:nil completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, BOOL finished){ + if (!wself) return; + void (^block)(void) = ^ + { + __strong UIImageView *sself = wself; + [sself stopAnimating]; + if (!sself) return; + if (image) + { + NSMutableArray *currentImages = [[sself animationImages] mutableCopy]; + if(!currentImages) currentImages = [[NSMutableArray alloc] init]; + [currentImages addObject:image]; + + sself.animationImages = currentImages; + [sself setNeedsLayout]; + } + [sself startAnimating]; + }; + if ([NSThread isMainThread]) + { + block(); + } + else + { + dispatch_sync(dispatch_get_main_queue(), block); + } + }]; + [operationsArray addObject:operation]; + } + + objc_setAssociatedObject(self, &operationArrayKey, [NSArray arrayWithArray:operationsArray], OBJC_ASSOCIATION_RETAIN_NONATOMIC); +} + - (void)cancelCurrentImageLoad { // Cancel in progress downloader from queue @@ -93,4 +134,18 @@ static char operationKey; } } +- (void)cancelCurrentArrayLoad +{ + // Cancel in progress downloader from queue + NSArray *operations = objc_getAssociatedObject(self, &operationArrayKey); + for(id operation in operations){ + if (operation) + { + [operation cancel]; + } + } + objc_setAssociatedObject(self, &operationArrayKey, nil, OBJC_ASSOCIATION_RETAIN_NONATOMIC); + +} + @end