【问题标题】:AsyncImageView not caching the imageAsyncImageView 不缓存图像
【发布时间】:2012-11-12 11:15:33
【问题描述】:

嗯,使用AsyncImageViewhere。在文档中,他们说“默认情况下,所有加载的图像都被缓存”。您可以在文档的最后一段看到它。

但是问题是当我尝试再次加载图像时,它带有占位符并等待几秒钟以再次从 URL 加载图像,即使上次已成功加载。 p>

这意味着它没有被缓存,对吧?
那么请问我的代码是否有问题?
为什么图像没有像使用SDWebImage 那样被缓存?

这是我的代码:

AsyncImageView *imageView = [[AsyncImageView alloc] initWithFrame:CGRectMake(0, 0, productCellWidth, productCellHeight)];
imageView.contentMode = UIViewContentModeScaleAspectFill;
imageView.clipsToBounds = YES;
NSURL *URL =  [NSURL URLWithString:[NSString stringWithFormat:ProductPicture]];
[AsyncImageLoader defaultCache];
[[AsyncImageLoader sharedLoader]loadImageWithURL:URL target:self success:@selector(successSelector) failure:@selector(failureSelector)];

【问题讨论】:

  • “缓存将继续建立,直到触发内存警告”。您是否触发了内存警告?

标签: objective-c ios caching uiimageview image-caching


【解决方案1】:

我以前使用过另一个库,它运行良好 这个库叫做“SDWebImage”,你可以简单地通过一个方法来使用它:

[imageView setImageWithURL:[NSURL URLWithString:@"http://www.domain.com/path/to/image.jpg"]
               placeholderImage:[UIImage imageNamed:@"placeholder.png"]];

它会自动缓存图像,并且只支持从缓存中加载(如果需要)。

【讨论】:

  • 好吧,现在正在使用它,它工作得很好,但是如何告诉它缓存的时间,这意味着如果我想清除缓存?我想每 4 天缓存一次新图像,所以我希望它每 4 天调用一次服务器,如何设置这些属性?
【解决方案2】:

在 SDImageCache.m 中你会发现这行代码:

static NSInteger cacheMaxCacheAge = 60*60*24*7; // 1 week

这是缓存图像的生命周期,您可以将其更改为 4 天而不是 7 天

【讨论】:

  • 或者你可以每4天调用一次[SDWebImageCache.sharedImageCache clearDisk]
【解决方案3】:

对我来说,您的代码看起来不对。您只需设置 AsyncImageView 的 imageURL。

你不处理加载器

【讨论】:

    【解决方案4】:

    我已经解决了这个问题:你需要添加

    #import "AsyncImageView.h"
    

    在你的文件 .m

    和使用

    NSURL *url = [NSURL URLWithString:yourUrlString];
    photo.imageURL = url; 
    

    或者举例

    cell.imageView.imageURL = url;
    

    不是

    UIImage *theImage = [UIImage imageWithData:imageData];
    photo.image = theImage;
    

    【讨论】:

      猜你喜欢
      • 2013-06-07
      • 1970-01-01
      • 2015-08-29
      • 2016-08-20
      • 2015-05-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多