我没有从远程服务器找到图像时加载默认图像:
detailImageView.image = [UIImage imageNamed:@"noimageavailable.jpg"];
Run Code Online (Sandbox Code Playgroud)
第二种选择:
detailImageView.image = [UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"noimageavailable" ofType:@"jpg"]];
Run Code Online (Sandbox Code Playgroud)
我注意到在加载了另一个可从服务器获得的图像后,noimageavailable.jpg仍然出现在新图像后面,这意味着noimageavailable.jpg以某种方式缓存.我用两个选项imageNamed:和imageWithContentsOfFileAPI 得到了相同的结果.
这是我完成的代码:
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;
});
});
}
Run Code Online (Sandbox Code Playgroud)
有什么方法可以清除缓存?
关于这两种方法
+ (UIImage *)imageNamed:(NSString *)name
此方法在系统缓存中查找具有指定名称的图像对象,并返回该对象(如果存在).如果匹配的图像对象尚未在缓存中,则此方法从指定的文件加载图像数据,对其进行缓存,然后返回结果对象.
无法清除此缓存
+ (UIImage *)imageWithContentsOfFile:(NSString *)path
Run Code Online (Sandbox Code Playgroud)
此方法不缓存图像对象.
但我认为,你的情况并不取决于任何图像缓存,你不会替换图像,但每次创建新的imageView并将其添加到上一个.
| 归档时间: |
|
| 查看次数: |
3643 次 |
| 最近记录: |