iOS:SDWebImageManager不缓存图像 [英] iOS: SDWebImageManager not caching image

查看:664
本文介绍了iOS:SDWebImageManager不缓存图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用UIImageView创建幻灯片,并且图像链接位于一个数组中,所以当我坐在幻灯片上时,我了解到SDWebImageManager只允许点击一次网址,然后将其缓存以备后用使用.

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.

但是我相信我在应用程序中正在监视的是第一张图片已缓存,但是第二张图片的URL总是被命中.

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.

这是我的代码:

- (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];
}

这是handleTimer代码,每5秒在图像视图中重新加载图像:

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:

如果我错误地使用了SDWebImageManager,请指导我.

Please guide me if I have used the SDWebImageManagerwrongly.

谢谢

推荐答案

我的母语是中文,而不是英语.也许我无法清楚表达自己的想法,但我会尽力告诉自己的想法.如果我让您感到困惑,我感到抱歉.

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.

我无法挂载SDWebimage,因为我的国家有时会封锁Google,所以我无法重现您的情况.尽管如此,我仍然给您一些建议,可能会对您有所帮助

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

首先,您提供的背景知识很少.也许您可以发布有关成员变量和属性的更多信息.当我将代码复制到Xcode时,我需要自己添加它们.

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.

第二,你的意思是当你使用
NSURL *imageURL = [NSURL URLWithString:[arry objectAtIndex:1]];,

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

sdwebimage每次都会访问url,而不使用缓存URL?您可以通过NSLog的cacheType获取图像源.

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表示图像来自网络.

SDImageCacheTypeNone means image comes from network.

SDImageCacheTypeDisk表示图像来自磁盘.

SDImageCacheTypeDisk means image comes from disk.

SDImageCacheTypeMemory表示图像来自内存.

SDImageCacheTypeMemory means image comes from Memory.

第三,因为downloadWithURL:options:completed:不在主线程上执行.我怀疑这个顺序与您的想法是否相同.

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

这篇关于iOS:SDWebImageManager不缓存图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆