From 9e3e566cfd0401839b121a91b76bf926353f9712 Mon Sep 17 00:00:00 2001 From: sean9keenan Date: Fri, 29 Jan 2016 15:17:00 -0800 Subject: [PATCH] Fixed issue where animated image arrays could be populated out of order --- SDWebImage/UIImageView+WebCache.m | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) diff --git a/SDWebImage/UIImageView+WebCache.m b/SDWebImage/UIImageView+WebCache.m index 81f687ef..4e5908f9 100644 --- a/SDWebImage/UIImageView+WebCache.m +++ b/SDWebImage/UIImageView+WebCache.m @@ -75,7 +75,7 @@ NSMutableArray> *operationsArray = [[NSMutableArray alloc] init]; - for (NSURL *logoImageURL in arrayOfURLs) { + [arrayOfURLs enumerateObjectsUsingBlock:^(NSURL *logoImageURL, NSUInteger idx, BOOL * _Nonnull stop) { id operation = [SDWebImageManager.sharedManager loadImageWithURL:logoImageURL options:0 progress:nil completed:^(UIImage *image, NSData *data, NSError *error, SDImageCacheType cacheType, BOOL finished, NSURL *imageURL) { if (!wself) return; dispatch_main_async_safe(^{ @@ -86,7 +86,16 @@ if (!currentImages) { currentImages = [[NSMutableArray alloc] init]; } - [currentImages addObject:image]; + + // We know what index objects should be at when they are returned so + // we will put the object at the index, filling any empty indexes + // with the image that was returned too "early". These images will + // be overwritten. (does not require additional sorting datastructure) + while ([currentImages count] < idx) { + [currentImages addObject:image]; + } + + currentImages[idx] = image; sself.animationImages = currentImages; [sself setNeedsLayout]; @@ -95,7 +104,7 @@ }); }]; [operationsArray addObject:operation]; - } + }]; [self sd_setImageLoadOperation:[operationsArray copy] forKey:@"UIImageViewAnimationImages"]; }