0

I am using SDWebImage to get pictures.

let's say I have two view controllers A and B. B is the next view of A. they need to show the same image:

[iconIamgeView setImageWithURL:[NSURL URLWithString:@"http://www.example.com/icon.jpg"];

I thought SDWebImage would cache http://www.example.com/icon.jpg after this code in A, so B was going to use the cache. Thus there should not be a detectable latency when I used this code to show the same image in B. However, it seems that I was wrong. there was an obvious latency when I wanted to show this image in B.

How can I use the cache properly? thanks!

Brian
  • 30,156
  • 15
  • 86
  • 87

2 Answers2

0

I am sorry that the problem I mentioned in the question was not the real problem.

[iconIamgeView setImageWithURL:[NSURL URLWithString:@"http://www.example.com/icon.jpg"];

this code does the cache work well.

the real problem is my code of setting image was not on the main thread, so I could not show the image immediately. I used dispatch_async to solve the problem:

dispatch_async(dispatch_get_main_queue(), ^{
    [iconIamgeView setImageWithURL:[NSURLURLWithString:@"http://www.example.com/icon.jpg"];
});

hope this answer may help someone who faces the same issue.

Brian
  • 30,156
  • 15
  • 86
  • 87
0
NSURL *imageURL = [NSURL URLWithString:[linkImageArray objectAtIndex:index]];
UIImage *placeholderImage = [UIImage imageNamed : [linkImageArray objectAtIndex: index]];

[imageView setImageWithURL:imageURL
          placeholderImage:placeholderImage
                   options:SDWebImageRefreshCached
                 completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType)
                   {
                       NSLog(@"image loaded");
                   }];
atticus
  • 960
  • 7
  • 10