我正在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.jpgcity5~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];

如果这些都不起作用,我怀疑您的问题是三件事之一:
  • 您要加载的图像在捆绑包中不存在。因此,请确保该图像确实在您的项目中,并通过单击文件并选择其所属的目标来确保已检查目标。
  • 确保您没有拼写错误的图片名称。
  • 或您正在使用视网膜显示器,但没有@ 2x图像。尝试将您的模拟器更改为视网膜,然后查看是否出现。

  • 作为最后的尝试,尝试做UIImage *backgroundImage = [UIImage imageNamed:@"city5~ipad"];
    您也可以尝试self.view.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"city5"]];

    08-15 20:32