Xcode UIImageView';更改图像时,s图像将消失

Xcode UIImageView';更改图像时,s图像将消失,xcode,ios,xcode4,Xcode,Ios,Xcode4,在代码中更改UIImageView上的图像时,图像将消失。这只发生在iPod Touch设备上,而不是iPhone模拟器上 在我的包中,我有一个名为SomeImage.PNG的图像 NSString *path = [[NSBundle mainBundle] pathForResource:@"SomeImage" ofType:@"png"]; [self.background setImage:[[UIImage alloc] initWithContentsOfFile:path]];

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

在我的包中,我有一个名为
SomeImage.PNG的图像

NSString *path = [[NSBundle mainBundle] pathForResource:@"SomeImage" ofType:@"png"];
[self.background setImage:[[UIImage alloc] initWithContentsOfFile:path]];
我必须重申,这在iPhone模拟器上运行良好,但在iPodtouch设备上不起作用

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

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

我在问是否有“更好”的方法来做我正在做的事情,这样我以后就不会遇到这样的问题了。

不太可能。不幸的是,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];
}

不是真的。不幸的是,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];
}

谢谢,这是一个很好的答案,但从长远来看,可能不必要的代码量。我已经吸取了教训!谢谢,这是一个很好的答案,但从长远来看,可能不必要的代码量。我已经吸取了教训!