简体   繁体   English

xcode-资产目录-图像类型-相同比例-不同分辨率

[英]xcode - asset catalog - image type - same scale - different resolutions

I have an iOS application which is developed years ago and sets backgrounds of many controls using images.xcassets catalog. 我有一个iOS应用程序,该应用程序是几年前开发的,并且使用images.xcassets目录设置了许多控件的背景。 The images are set using contents.json in which targets are iPhone, iPad using 'idiom' key. 使用contents.json设置图像,其中目标是使用“ idiom”键的iPhone,iPad。 Further specifications include 'scale' & 'filename'. 其他规范包括“比例”和“文件名”。 Seems that this app worked fine until iOS 8. 似乎该应用在iOS 8之前运行良好。

As of today there are many high screen resolution devices of iPhone, iPad. 截止到今天,有许多iPhone,iPad的高屏幕分辨率设备。 So, I need to add further more images targeting these high screen resolution devices. 因此,我需要添加更多针对这些高屏幕分辨率设备的图像。

I don't know how it is handled earlier, but now there are different screen resolutions in the same scale factor. 我不知道它的早期处理方式,但是现在在相同的比例系数下有不同的屏幕分辨率。 For eg, iPad Pro 12.9" & iPad Pro 11" have '2x' as scale factor but has different resolutions '2048x2732' & '1668x2388' respectively. 例如,iPad Pro 12.9“和iPad Pro 11”的比例因子为“ 2x”,但分辨率分别为“ 2048x2732”和“ 1668x2388”。 Now if I create an image targeting highest resolution 2048x2732, the image is not getting centered for resolution 1668x2388. 现在,如果我创建的图像的最高分辨率为2048x2732,则该图像的中心位置不是1668x2388。

  1. Is there any particular key in contents.json that can differentiate screen resolutions/sizes for the same scale factor? content.json中是否有任何特定键可以区分相同比例因子的屏幕分辨率/尺寸?
  2. When I search for Apple's documentation for asset catalog it is found in this documentation archive . 当我搜索Apple的资产目录文档时,可以在此文档档案中找到它。 Does that mean we shouldn't use asset catalog for images any more? 这是否意味着我们不应再将资产目录用于图像? Are there any latest standards for setting background images? 设置背景图像是否有最新标准?

There are two options you can follow for setting background images for different devices. 您可以遵循两个选项来设置不同设备的背景图像。

1-) Setting UIImageView content mode to .aspectFill . 1-)将UIImageView内容模式设置为.aspectFill By settings this property, your image will be croped for different aspect ratios but original aspect ratio will be protected. 通过设置此属性,将为您的图像裁剪不同的长宽比,但原始长宽比将受到保护。 I recommend this option If you use repetitive images for background. 如果您将重复图像用作背景,则建议使用此选项。

2-) Setting two different asset file for different aspect ratio and resolution. 2-)设置两个不同的资产文件以实现不同的宽高比和分辨率。 If you follow this option, in code, you need to check for the device and set the proper image asset. 如果遵循此选项,则在代码中,您需要检查设备并设置正确的图像资产。

let image: UIImage!
if UIDevice().userInterfaceIdiom == .phone {
switch UIScreen.main.nativeBounds.height {
    case 1920, 2208:
        print("iPhone 6+/6S+/7+/8+")
        image = UIImage(named:"StandartBackground.png")
    case 2436:
        print("iPhone X, XS")
        image = UIImage(named:"XBackground.png")
    case 2688:
        print("iPhone XS Max")
        image = UIImage(named:"XSBackground.png")
    }
}

Advice: Common and proper way is first option but there will be cases which won't suite it. 建议:常见且正确的方法是第一选择,但在某些情况下将不适合它。 Therefore, try to avoid second option but If have no other option. 因此,尝试避免第二种选择,但是如果没有其他选择。 Put every different aspect ratio to different asset file. 将每个不同的宽高比放置到不同的资产文件中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM