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

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

问题描述

我正在使用 UIImageView 创建幻灯片,并且图像链接在一个数组中,所以当我这样做时,我了解到 SDWebImageManager 可以点击 URL只有一次,然后它会缓存图像以供以后使用.

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,因为我的国家有时会阻止谷歌,所以我无法重现你的场景.虽然我仍然给你一些建议,可能会对你有所帮助

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天全站免登陆