我正在使用SDWebImage将图像加载到数组中。我需要按照我在for循环中调用它们的顺序加载这些图像,但是因为有一些图像被缓存,所以它们被放置在正在下载的图像之前:
2013-08-20 17:45:51.318 img of index 0. Inserted at 0 / 1 with cacheType of 1
2013-08-20 17:45:51.336 img of index 1. Inserted at 1 / 2 with cacheType of 1
2013-08-20 17:45:51.350 img of index 2. Inserted at 2 / 3 with cacheType of 1
2013-08-20 17:45:51.391 img of index 4. Inserted at 3 / 4 with cacheType of 1
2013-08-20 17:45:51.402 img of index 6. Inserted at 4 / 5 with cacheType of 1
2013-08-20 17:45:51.415 img of index 7. Inserted at 5 / 6 with cacheType of 1
2013-08-20 17:45:51.419 img of index 8. Inserted at 6 / 7 with cacheType of 1
2013-08-20 17:45:51.428 img of index 9. Inserted at 7 / 8 with cacheType of 1
2013-08-20 17:45:54.982 img of index 3. Inserted at 8 / 9 with cacheType of 0
2013-08-20 17:45:56.840 img of index 5. Inserted at 9 / 10 with cacheType of 0
缓存类型:
/**
* The image wasn't available the SDWebImage caches, but was downloaded from the web.
*/
SDImageCacheTypeNone = 0,
/**
* The image was obtained from the disk cache.
*/
SDImageCacheTypeDisk,
/**
* The image was obtained from the memory cache.
*/
SDImageCacheTypeMemory
我的方法:
//Web loading
_manager = [SDWebImageManager sharedManager];
_manager.imageDownloader.maxConcurrentDownloads = 1;
-(void)loadImages:(NSMutableArray*)imagesURLS{
//_indexOfLastImageLoaded = 0;
[_loadedImages removeAllObjects];
_loadedImages = [[NSMutableArray alloc]init];;
for (int i=0;i<imagesURLS.count;i++){
[_manager downloadWithURL:[imagesURLS objectAtIndex:i]
options:0
progress:^(NSUInteger receivedSize, long long expectedSize)
{
//NSLog(@"%i out of %lli",receivedSize,expectedSize);
}
completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType,BOOL finished)
{
if (image && finished)
{
[_loadedImages addObject:image];
NSLog(@"img of index %i. Inserted at %i / %i with cacheType of %i",i,[_loadedImages indexOfObject:image],_loadedImages.count, cacheType);
}
}];
}
}
在以下情况下检查数组的计数:
if(_loadedImages.count > _indexOfImageBeingDisplayed ){
NSLog(@"Top IV img loaded from array at index %i",_indexOfImageBeingDisplayed);
[_imagePreviewerTop setImage:[_loadedImages objectAtIndex:_indexOfImageBeingDisplayed]];
[_imagePreviewerTop setFrame:_borderFrame];
}
图像的重新排序正在弄乱我试图向用户显示的信息。我也试过改变“选项”参数,但这也没有用。
任何帮助都将不胜感激。
谢谢!
答案 0 :(得分:1)
您可以考虑使用replaceObjectAtIndex:WithObject
代替addObject
。
...
if (image && finished) {
[_loadedImages replaceObjectAtIndex:i withObject:image];
}
...
为此,您必须先使用占位符值初始化数组。
_loadedImages = [NSMutableArray arrayWithCapacity:imagesURLS.count];
for (NSUInteger i = 0; i < imagesURLS.count; i++) {
[_loadedImages addObject:[NSNull null];
}