【问题标题】:imageNamed and imageWithContentsOfFile cache image: Anyway to clear cache?imageNamed 和 imageWithContentsOfFile 缓存图像:无论如何要清除缓存?
【发布时间】:2013-07-23 03:17:33
【问题描述】:

当没有从远程服务器找到图像时,我正在加载默认图像:

   detailImageView.image = [UIImage imageNamed:@"noimageavailable.jpg"];

第二个选项:

detailImageView.image = [UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"noimageavailable" ofType:@"jpg"]];

我注意到在从服务器加载另一个可用的图像后,noimageavailable.jpg 仍然出现在新图像后面,这意味着noimageavailable.jpg 以某种方式被缓存。我使用imageNamed: 和imageWithContentsOfFile API 两个选项得到了相同的结果。

这是我完成的代码:

        if (detailImageURL.length == 0) {
            detailImageView.image = [UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"noimageavailable" ofType:@"jpg"]];
//Once displayed, it's cached for all lifecycle


        }else{

        dispatch_async(DownloadQueue, ^{

            NSData *imageData = [NSData dataWithContentsOfURL:[NSURL URLWithString:detailImageURL]];
                dispatch_async(dispatch_get_main_queue(), ^{

                    detailImageView.image = nil;
                    UIImage *image = [UIImage imageWithData:imageData];
                    detailImageView.image = image;
                });
    });
    }

有什么办法可以清除缓存?

【问题讨论】:

    标签: ios uiimage uikit


    【解决方案1】:

    关于这两个methods

    + (UIImage *)imageNamed:(NSString *)name

    此方法在系统缓存中查找具有指定名称的图像对象,如果该对象存在,则返回该对象。如果匹配的图像对象尚未在缓存中,则此方法从指定文件加载图像数据,缓存,然后返回结果对象。

    没有办法清除这个缓存

    + (UIImage *)imageWithContentsOfFile:(NSString *)path
    

    此方法不缓存图像对象。

    但是我认为,您的情况不依赖于任何图像缓存并且您不替换图像,而是每次创建新的 imageView 并将其添加到上一个。

    【讨论】:

      【解决方案2】:

      您是否可以添加多个单独的 UIImageView? IE。当调用此方法时,您正在向您的超级视图添加一个名为 detailImageView 的局部变量,然后您再次调用此方法,并将一个名为 detailImageView 的新局部变量 UIImageView 添加到旧视图之上的超级视图一?如果是这种情况,您应该会看到 [self.view.subviews count] 随着该方法被多次调用而增加,添加了多个 UIImageView。在这种情况下,您需要在添加新 UIImageView 之前删除旧的 UIImageView。

      【讨论】:

        猜你喜欢
        • 2011-02-22
        • 2013-06-23
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-07-04
        • 2017-09-18
        • 2011-04-12
        • 2011-02-25
        相关资源
        最近更新 更多