更改多个平台的背景图片 iOS

Changing background image for multiple platforms iOS

在移动开发方面,我仍未涉足。

所以我正在开发一个只有一个普通视图控制器的 .xib。我的平面设计师给了我 6 个不同的背景图像作为单独的文件供我使用。一种用于 iPhone 4s,一种用于 5&5s,一种用于 6,一种用于 6+,一种用于非视网膜 iPad,一种用于视网膜 iPad。 .xib 和图像位于 CocoaPod 中,作为我们制作的几个不同应用程序之间的共享库。

我的问题是:如何正确确定将哪个图像设置为背景,因为该应用程序可以 运行 的设备都需要不同的图像?

到目前为止我尝试过的:

获取设备

-(NSString*)deviceName {
    struct utsname systemInfo;
    uname(&systemInfo);

//I set deviceType to the following return
return [NSString stringWithCString:systemInfo.machine
                          encoding:NSUTF8StringEncoding];
}

然后是相当多的 if 语句来设置背景图片:

if ([deviceType compare:@"iPhone3, 1"] == 0) {
    [self.view setBackgroundColor:[UIColor colorWithPatternImage:[UIImage imageNamed:@"iPhone_4"]]];
}
else if ([deviceType compare:@"iPhone5, 4"] == 0) {
    [self.view setBackgroundColor:[UIColor colorWithPatternImage:[UIImage imageNamed:@"iPhone_5"]]];
}

如果要为不同的屏幕尺寸指定不同的图像,解决方案之一是名称图像最后加上屏幕尺寸高度。

例如图片"DetailsBackground"

对于iPhone 4, 4s - "DetailsBackground480"

iPhone 5, 5s - "DetailsBackground568"

iPhone 6 - "DetailsBackground667"

等等

然后在代码中根据您的屏幕边界加载图像:

NSString *imageName = [NSString stringWithFormat:@"BackgroundImage%.0f.png", [UIScreen mainScreen].bounds.size.height];
UIImage *backgroundImage = [UIImage imageNamed:imageName];
self.view.backgroundColor = [UIColor colorWithPatternImage:backgroundImage];

另一种解决方案是使用图像资源。

但它更适合portrait/landscape和iPhone/iPad 你可以在图像属性中使用"Device Specific" + compact\regular\any widht+height

请查看附件截图: