gpt4 book ai didi

ios - imageNamed 和 imageWithContentsOfFile 缓存图像 : Anyway to clear cache?

转载 作者:行者123 更新时间:2023-11-28 18:36:35 24 4
gpt4 key购买 nike

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

   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;
});
});
}

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

最佳答案

关于这 2 methods

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

This method looks in the system caches for an image object with the specified name and returns that object if it exists. If a matching image object is not already in the cache, this method loads the image data from the specified file, caches it, and then returns the resulting object.

无法清除此缓存

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

This method does not cache the image object.

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

关于ios - imageNamed 和 imageWithContentsOfFile 缓存图像 : Anyway to clear cache?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17800937/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com