2012-12-17 55 views
1

我有以下代码忽略对setImage图像缓存:

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"] 一次,它认为这是同一个。如何“刷新”缓存并告诉它重新加载图像?

回答

0

setImage:从缓存中加载(,正如您正确猜测)。 寻找它在“的UIImage类参考” (在Xcode的组织者窗口)

几行降低IT 你答案:使用initWithContentsOfFile:方法。 所以,当你改变了形象做到这一点:

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

现在imgView将有新的图像(只要一个IMG文件存在于myAbsolutePath)。

+0

Gr8!有效。谢谢。 – Georgia