1

我有以下代码

NSString *myPath = [NSString stringWithFormat:@"../Documents/%@",@"bla.png"];
[imgView setImage:[UIImage imageNamed:myPath]];    // works fine
[self updateMyImage:[UIImage imageNamed:myPath]];  // loads another image as bla.png (works fine)
[imgView setImage:[UIImage imageNamed:myPath]];    // displays the old image, *not* the new one

图像在 updateMyImage 中加载:(我打开它(从模拟器的磁盘),它是“”的)但 imgView 显示“”的。我猜它是一个缓存问题。由于iOS已经加载了[UIImage imageNamed:@"bla.png"] 一次,它认为这是同一个。如何“刷新”缓存并告诉它重新加载图像?

4

1 回答 1

0

setImage:从缓存中加载(正如您正确猜测的那样)。在“UIImage 类参考”中查找它(在 xcode 的管理器窗口中)

它下面几行你答案:使用 initWithContentsOfFile: 方法。因此,在更改图像后,请执行以下操作:

 NSString *myAbsolutePath = [NSString stringWithFormat:@"%@/%@", [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0],@"bla.png"]];
 UIImage *myImg = [[UIImage alloc] initWithContentsOfFile:myAbsolutePath];
 [imgView setImage:myImg];

现在 imgView 将拥有新图像(只要 myAbsolutePath 中存在 img 文件)。

于 2012-12-17T15:27:46.207 回答