我正在xcode 5中开发通用应用程序,而我正在尝试为两个应用程序设置背景图像。我使用的代码在viewDidLoad
方法中:
UIImage *backgroundImage = [UIImage imageNamed:@"city5.jpg"];
UIImageView *backgroundImageView=[[UIImageView alloc]initWithFrame:self.view.frame];
backgroundImageView.image=backgroundImage;
[self.view insertSubview:backgroundImageView atIndex:0];
这两个图像的名称分别为
city5~iphone.jpg
和city5~ipad.jpg
。图像可以正常使用iphone。但是,ipad图像永远不会加载,视图只是保持空白。我正在ipad 2上进行部署。
最佳答案
您的问题是在使用UIImage *backgroundImage = [UIImage imageNamed:@"city5.jpg"];
的imageNamed:@"city5.jpg"
中,特别是imageNamed:
中,它会查找的是.png
图像而不是.jpg
图像,因此从本质上讲,您正在寻找的city5.jpg.png
文件显然是您想要的,因此将其更改为UIImage *backgroundImage = [UIImage imageNamed:@"city5"];
并将其图像文件更改为是.png
。
如果您想将其保留为.jpg
,请尝试以下操作。
NSString *filePath = [[NSBundle mainBundle] pathForResource:@"city5" ofType:@"jpg"];
UIImage *backgroundImage = [UIImage imageWithContentsOfFile:filePath];
如果这些都不起作用,我怀疑您的问题是三件事之一:
作为最后的尝试,尝试做
UIImage *backgroundImage = [UIImage imageNamed:@"city5~ipad"];
您也可以尝试
self.view.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"city5"]];