在代码中更改 UIImageView 上的图像时,图像会消失。这只会发生在 iPod Touch 设备上,不会发生在 iPhone 模拟器上。
在我的包中,我有一个名为 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]];
我在问是否有“更好”的方法来做我正在做的事情,这样我以后就不会遇到此类问题。
最佳答案
不是真的。不幸的是,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];
}
关于xcode - 更改图像时 UIImageView 图像消失,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5926023/