iOS: SDWebImageManager not caching image

496 views Asked by At

I'm creating a slideshow using UIImageView, and the image links are in an array , so while I was at it, I learned that SDWebImageManager lets hit the URLs once only and then it caches the images for later use.

But what I'm monitoring in my app is that the 1st image is cached, I believe, but the 2nd image URL is always being hit.

Here's my code:

- (void)viewDidLoad {
    [super viewDidLoad];

    arry = [[NSMutableArray alloc] init];
    [arry addObject:@"http://adjingo.2cimple.com/content/151/Image/6291.jpg"];
    [arry addObject:@"http://adjingo.2cimple.com/content/151/Image/6290.jpg"];

    NSURL *imageURL = [NSURL URLWithString:[arry objectAtIndex:0]];

    __block UIActivityIndicatorView *activityIndicator;
    __weak UIImageView *weakImageView = self.imageView;
    SDWebImageManager *manager = [SDWebImageManager sharedManager];
    [manager downloadImageWithURL:imageURL
                      options:0
                     progress:^(NSInteger receivedSize, NSInteger expectedSize) {
                         // progression tracking code
                         if (!activityIndicator) {
                             [weakImageView addSubview:activityIndicator = [UIActivityIndicatorView.alloc initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray]];
                             //activityIndicator.center = self.imageView.center;
                             [activityIndicator startAnimating];
                         }
                     }
                    completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, BOOL finished, NSURL *imageURL) {
                        if (image) {
                            // do something with image
                            [activityIndicator removeFromSuperview];
                            activityIndicator = nil;
                            [self.imageView setImage:image];
                        }
                    }];

//Timer to do slideshow for images
timer = [NSTimer scheduledTimerWithTimeInterval: 5.0
                                             target: self
                                           selector: @selector(handleTimer:)
                                           userInfo: nil
                                            repeats: YES];
}

Here's the handleTimer code, to reload image in image view, every 5 seconds:

-(void) handleTimer: (NSTimer *) timer {

        currentImage++;
        if ( currentImage >= arry.count )
            currentImage = 0;

        __block UIActivityIndicatorView *activityIndicator;
        __weak UIImageView *weakImageView = self.imageView;
        SDWebImageManager *manager = [SDWebImageManager sharedManager];
        [manager downloadImageWithURL:imageURL
                      options:0
                     progress:^(NSInteger receivedSize, NSInteger expectedSize) {
                         // progression tracking code
                         if (!activityIndicator) {
                             [weakImageView addSubview:activityIndicator = [UIActivityIndicatorView.alloc initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray]];
                             //activityIndicator.center = self.imageView.center;
                             [activityIndicator startAnimating];
                         }
                     }
                    completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, BOOL finished, NSURL *imageURL) {
                        if (image) {
                            // do something with image
                            [activityIndicator removeFromSuperview];
                            activityIndicator = nil;
                            [self.imageView setImage:image];
                        }
                    }];
}

Here's where I monitor the network usage: enter image description here

Please guide me if I have used the SDWebImageManagerwrongly.

Thanks

1

There are 1 answers

0
jeff.zhou On

my mother language is Chinese,not English.Maybe I cannot express my thought clearly,while I will try my best to tell my idea.if I confuse you , I feel sorry.

I cannot pod SDWebimage because my country blocks google sometimes,so I cannot reproduce your scenarios.While I still give your some advice which may help you

First of all, your gave us little context.Maybe you can post more information about member variables and properties.When I copy your code to the Xcode.I need add them by myself.

Second,you mean when you use
NSURL *imageURL = [NSURL URLWithString:[arry objectAtIndex:1]];,

sdwebimage hits urls every time , not use cache URLs? you can get the image source by NSLog the cacheType.

completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, BOOL finished, NSURL *imageURL) {  
                      if (image) {                                                    
                            NSLog(@"%d",cacheType);
                          [activityIndicator removeFromSuperview];
                          activityIndicator = nil;
                          [self.imageView setImage:image];
                     }
                  }];`

SDImageCacheTypeNone means image comes from network.

SDImageCacheTypeDisk means image comes from disk.

SDImageCacheTypeMemory means image comes from Memory.

Third,because the downloadWithURL:options:completed: is excuted not on the main thread. I doubt the sequence is the same with your thought.