load images using SDWebImage in order

江枫思渺然 提交于 2019-12-11 19:39:36

问题


After a lot of reading and recommendations, I am trying to implement SDWebImage framework in my app. I am able to download images from the server but the issue is that as the images are different sizes, they are not downloaded in the order that I call them (and would like them to be).

Here is the framework for reference: https://github.com/rs/SDWebImage

As you can see evidence of this from the print-line:

2013-08-20 11:11:01.785 Project[10785:c07] SD LOAD 1 array count = 1
2013-08-20 11:11:02.668 Project[10785:c07] SD LOAD 0 array count = 2
2013-08-20 11:11:02.946 Project[10785:c07] SD LOAD 3 array count = 3
2013-08-20 11:11:03.306 Project[10785:c07] SD LOAD 2 array count = 4
2013-08-20 11:11:03.862 Project[10785:c07] SD LOAD 4 array count = 5
2013-08-20 11:11:04.094 Project[10785:c07] SD LOAD 5 array count = 6
2013-08-20 11:11:04.793 Project[10785:c07] SD LOAD 6 array count = 7
2013-08-20 11:11:04.837 Project[10785:c07] SD LOAD 7 array count = 8
2013-08-20 11:11:05.046 Project[10785:c07] SD LOAD 8 array count = 9
2013-08-20 11:11:05.428 Project[10785:c07] SD LOAD 9 array count = 10

Below is my method:

-(void)sdWebLoadImages:(NSMutableArray*)imagesURLS{

    [_loadedImages removeAllObjects];
    NSMutableArray *tempArray = [[NSMutableArray alloc]init];
    _loadedImages = tempArray;
    for (int i=0;i<imagesURLS.count;i++){

    [_manager downloadWithURL:[imagesURLS objectAtIndex:i]
                     options:0
                    progress:^(NSUInteger receivedSize, long long expectedSize)
     {
         // progression tracking code
     }
                   completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType,BOOL finished)
     {
         if (image && finished)
         {

             [_loadedImages addObject:image];
            NSLog(@"SD LOAD %i array count = %i",i,_loadedImages.count);
         }
     }];
    }
}

How can I fix this (is it something in my for loop logic?). Also, I am trying to cancel all the downloads when a button is clicked, and am using [_manager cancelAll] but doesn't seem to work - any idea why?

Thanks fam!

ADDED:

The issue now is that the manager inserts all the cached images into the array first, causing the order to be disrupted, even after I set the setMaxConcurrentOperationCount to 1. Not sure how to keep the downloading in order even with cached images.


回答1:


As simple as ice and as pointed by 'Justin Amberson':

Go to SDWebImageDownloader.m

I am assuming you are using the latest SDWebImage library.

On line 72:

_downloadQueue.maxConcurrentOperationCount = NSOperationQueueDefaultMaxConcurrentOperationCount;

Change it to:

_downloadQueue.maxConcurrentOperationCount = 1;

It will download one image at a time.

Cheers



来源:https://stackoverflow.com/questions/18342172/load-images-using-sdwebimage-in-order

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!