xcode - 更改图像时 UIImageView 图像消失

标签 xcode ios xcode4

在代码中更改 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/

相关文章:

iphone - 第一次使用 swift(不是 objective-c)启动 iPhone 应用程序时,如何播放介绍视频?

ios - libiconv 没有链接到 iOS 项目

ios - 如何将触摸从 UIScrollView 移动到 UITableViewCell?

objective-c - 警告 : duplicate protocol definition of '...' is ignored

ios - XCode4 - 排列排序组

ios - 代码 'Info.plist path not found.'

ios - 在扩展中有网点

c++ - 在 Xcode 3 中链接库

ios - MapKit 没有以正确的位置为中心

xcode - 为什么我无法将 Facebook SDK 拖到新项目中?