【问题标题】:iOS: SDWebImageManager not caching imageiOS:SDWebImageManager 不缓存图像
【发布时间】:2015-08-29 09:25:54
【问题描述】:

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

但是我在我的应用程序中监控的是第一张图片被缓存了,我相信,但第二张图片的 URL 总是被点击。

这是我的代码:

- (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 秒一次:

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

这是我监控网络使用情况的地方:

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

谢谢

【问题讨论】:

    标签: objective-c caching uiimageview slideshow sdwebimage


    【解决方案1】:

    我的母语是中文,不是英文。可能我的想法表达不清楚,但我会尽力说出我的想法。如果我混淆了你,我很抱歉。

    我无法播放 SDWebimage,因为我的国家/地区有时会阻止 google,所以我无法重现您的场景。虽然我仍然给您一些可能对您有所帮助的建议

    首先,您给了我们一些上下文。也许您可以发布有关成员变量和属性的更多信息。当我将您的代码复制到 Xcode 时。我需要自己添加它们。

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

    sdwebimage 每次都命中 url,不使用缓存 URL?可以通过 NSLog 的 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 表示图片来自网络。

    SDImageCacheTypeDisk 表示图像来自磁盘。

    SDImageCacheTypeMemory 表示图片来自内存。

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-11-12
      • 2016-08-20
      • 2015-05-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多