jamil
jamil

Reputation: 2437

How to download web images in Cache before displaying using SDWebimage?

i have to download large number of web images in my app. now i am trying to download images in my initial class using Grand Central Dispatch.

code:

- (void)viewDidLoad
{
[super viewDidLoad];

dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0ul);
    dispatch_async(queue, ^{
    // Perform non main thread operation
    for (NSString *imageURL in tempoaryArr1) {
        NSURL *url = [NSURL URLWithString:imageURL];

        SDWebImageManager *manager = [SDWebImageManager sharedManager];

        [manager downloadWithURL:url
                        delegate:self
                         options:0
                         success:^(UIImage *image, BOOL cached)
         {

         }
                         failure:nil];


    }
    dispatch_sync(dispatch_get_main_queue(), ^{
        // perform main thread operation
    });
});
}

But the above code only download 30 to 40 images at the first launch of app after that it stop downloading images in cache until i stop the app and run it again then the second time it download the some more images in cache and after that stop downloading so on.

So my question is why it stop after downloading some images,i want that it keeps downloading images in cache until it download all the images.

Upvotes: 3

Views: 3294

Answers (2)

Kausik Jati
Kausik Jati

Reputation: 49

SDWebImageManager *manager = [SDWebImageManager sharedManager];

[manager downloadImageWithURL:Imageurl options:0 progress:^(NSInteger receivedSize, NSInteger expectedSize)

{


} completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, BOOL finished, NSURL *imageURL) {

    if(image){
        NSLog(@"image=====%@",image);
    }
}]

Upvotes: 1

Michael Dautermann
Michael Dautermann

Reputation: 89509

So you're doing this downloading in a view controller (as evidenced by you putting it in a "viewDidLoad" method).

What is likely happening is that when you move to another view, or when the view controller is deallocated from memory, is that the "manager" object that you are using in that dispatch_queue is also being released when the view controller is leaving memory as well.

That's why the download stops.

To prove that I am right, leave the view on screen that is doing all this GCD work. I bet you'll get all your images, not just 30 or 40.

What you need to do is move this code to somewhere where it can live until it's done downloading (perhaps not a view controller, which is only on the screen for as long as the user wants to see it)?

Upvotes: 2

Related Questions