【发布时间】: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;
});
});
}
有什么办法可以清除缓存?
【问题讨论】: