Authored by Bogdan Poplauschi
Committed by GitHub

Merge pull request #1452 from sean9keenan/fixedOutOfOrderImages

Fixed issue where animated image arrays could be populated out of order
@@ -75,7 +75,7 @@ @@ -75,7 +75,7 @@
75 75
76 NSMutableArray<id<SDWebImageOperation>> *operationsArray = [[NSMutableArray alloc] init]; 76 NSMutableArray<id<SDWebImageOperation>> *operationsArray = [[NSMutableArray alloc] init];
77 77
78 - for (NSURL *logoImageURL in arrayOfURLs) { 78 + [arrayOfURLs enumerateObjectsUsingBlock:^(NSURL *logoImageURL, NSUInteger idx, BOOL * _Nonnull stop) {
79 id <SDWebImageOperation> operation = [SDWebImageManager.sharedManager loadImageWithURL:logoImageURL options:0 progress:nil completed:^(UIImage *image, NSData *data, NSError *error, SDImageCacheType cacheType, BOOL finished, NSURL *imageURL) { 79 id <SDWebImageOperation> operation = [SDWebImageManager.sharedManager loadImageWithURL:logoImageURL options:0 progress:nil completed:^(UIImage *image, NSData *data, NSError *error, SDImageCacheType cacheType, BOOL finished, NSURL *imageURL) {
80 if (!wself) return; 80 if (!wself) return;
81 dispatch_main_async_safe(^{ 81 dispatch_main_async_safe(^{
@@ -86,7 +86,16 @@ @@ -86,7 +86,16 @@
86 if (!currentImages) { 86 if (!currentImages) {
87 currentImages = [[NSMutableArray alloc] init]; 87 currentImages = [[NSMutableArray alloc] init];
88 } 88 }
89 - [currentImages addObject:image]; 89 +
  90 + // We know what index objects should be at when they are returned so
  91 + // we will put the object at the index, filling any empty indexes
  92 + // with the image that was returned too "early". These images will
  93 + // be overwritten. (does not require additional sorting datastructure)
  94 + while ([currentImages count] < idx) {
  95 + [currentImages addObject:image];
  96 + }
  97 +
  98 + currentImages[idx] = image;
90 99
91 sself.animationImages = currentImages; 100 sself.animationImages = currentImages;
92 [sself setNeedsLayout]; 101 [sself setNeedsLayout];
@@ -95,7 +104,7 @@ @@ -95,7 +104,7 @@
95 }); 104 });
96 }]; 105 }];
97 [operationsArray addObject:operation]; 106 [operationsArray addObject:operation];
98 - } 107 + }];
99 108
100 [self sd_setImageLoadOperation:[operationsArray copy] forKey:@"UIImageViewAnimationImages"]; 109 [self sd_setImageLoadOperation:[operationsArray copy] forKey:@"UIImageViewAnimationImages"];
101 } 110 }