2011-05-08 66 views
1

在代码中更改UIImageView上的图像时,图像消失。这只发生在iPod Touch设备上,而不发生在iPhone模拟器上。更改图像时,UIImageView的图像消失

在我的包我有一个名为SomeImage.PNG

NSString *path = [[NSBundle mainBundle] pathForResource:@"SomeImage" ofType:@"png"]; 
[self.background setImage:[[UIImage alloc] initWithContentsOfFile:path]]; 

我必须重申的图像,能正常工作在iPhone模拟器,但不是iPod Touch的设备上。

请注意,图像文件有一个大写的文件扩展名,但在我的代码中它是用小写声明。这就是问题。因此,要解决这个问题,将代码改成这样:

NSString *path = [[NSBundle mainBundle] pathForResource:@"SomeImage" ofType:@"PNG"]; 
[self.background setImage:[[UIImage alloc] initWithContentsOfFile:path]]; 

我问,如果有一个“好”的方式做我在做什么,所以我不碰到这样的问题在未来到来。

回答

1

不是。不幸的是,iOS文件系统区分大小写。

这样的事情.....?

NSString *filename = @"SomeImage"; 
NSString *extension = @"png"; 
UIImage *img = nil; 
NSString *path = nil; 

path = [[NSBundle mainBundle] pathForResource:filename ofType:[extension lowercaseString]]; 
img = [[UIImage alloc] initWithContentsOfFile:path]; 

if (img == nil) { 
    path = [[NSBundle mainBundle] pathForResource:filename ofType:[extension uppercaseString]]; 
    img = [[UIImage alloc] initWithContentsOfFile:path]; 
} 
+0

谢谢,一个很好的答案,但从长远来看可能是不必要的代码量。我已经吸取了教训! – Rots 2011-05-14 06:53:06