ios - 下载和使用缓存图像时按顺序将图像加载到数组中

标签 ios objective-c

我使用 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];
            }

图像的重新排序弄乱了我试图向用户显示的信息。我还尝试更改“选项”参数,但这也不起作用。

如有任何帮助,我们将不胜感激。

谢谢!

最佳答案

您可能会考虑使用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];
}

关于ios - 下载和使用缓存图像时按顺序将图像加载到数组中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18347293/

相关文章:

iphone - 如何使用MonoTouch实现类似于Photos应用程序的UI

ios - 如何在 Objective-C 中确定相同的 NSString?

ios - 启动应用程序时检测 iPad 方向

objective-c - NSDecimalNumber、NSDecimalNumberHandler 和线程

ios - 如何限制从顶部定位我的标签,但在我隐藏导航栏时不让它移动(因为它调整 View 高度)?

ios - ionic framework 2.0 事件推送通知未找到

ios - 如何以某种通用方式删除有序的对多 CoreData 关系中的对象?

objective-c - objective-c : Shake problems

objective-c - 如何在 Interface Builder 中使用 Storyboard连接页面 View Controller 的数据源导出

iphone - 如何获取我上传到 YouTube 的视频的链接