1

所以,我正在制作一个应用程序,其中包含大量用于 UI 的图像,但我在尝试设置框架时遇到了 @1x、2x 和 3x 概念的问题。

所以首先,我编写了一个全局函数库,其中包含一个名为的函数,getDevice这样我就可以确定用户使用什么设备来为每个元素设置正确的帧:

//Device
+ (int)getDevice
{
    CGSize bounds = [[UIScreen mainScreen] bounds].size;

    if((bounds.width == 320 && bounds.height == 480) || (bounds.width == 480 && bounds.height == 320))
        return gDevice_iPhone4S;
    else if((bounds.width == 320 && bounds.height == 568) || (bounds.width == 568 && bounds.height == 320))
        return gDevice_iPhone5;
    else if((bounds.width == 375 && bounds.height == 667) || (bounds.width == 667 && bounds.height == 375))
        return gDevice_iPhone6;
    else if((bounds.width == 414 && bounds.height == 736) || (bounds.width == 736 && bounds.height == 414))
        return gDevice_iPhone6Plus;
    else if((bounds.width == 768 && bounds.height == 1024) || (bounds.width == 1024 && bounds.height == 768))
        return gDevice_iPad;

    return gDevice_Unknown;
}

这一切都运行良好,但我的问题是围绕框架大小,现在我可以识别正确的设备。例如,假设我有三个按钮,我想跨越屏幕底部。在 iPhone 4S 上,它会是这样的:

iPhone 4S

现在这是使用 3 张图片:

  • image_1@2x.png (220x80)
  • image_2@2x.png (200x80)
  • image_3@2x.png (220x80)

但是当我想在 iPhone 6 上使用这些图像时会发生什么?

iPhone 6

现在这应该使用相同的图像,对吧,因为 iPhone 6 仍然使用@2x?

  • image_1@2x.png (220x80) -> (258x94)

但随后(220x80)需要成为(258x94)这将拉伸图像。那么当我需要@1x、@2x、@iPhone62x 和@iPhone6Plus3x 时,拥有@1x、@2x 和@3x iPhone 图像有什么意义呢?

4

1 回答 1

1

是的,乍一看这有点令人困惑。

iPhone 4/4s/5/5s 和 iPhone 6 都具有@2x 的意义在于这些屏幕的 DPI 相等(326 ppi)。

另一方面,您需要为这些 iPhone 提供不同的图像是特定于您的应用程序设计的,您有以下解决方案:

  • 使用创建的可拉伸图像-resizableImageWithCapInsets:并使图像在 110pt 和 129pt 时看起来都很好
  • 或者,如果不可能根据正在运行的设备应用程序使用不同的图像。您可以使用您的方法+getDevice(或类似方法)来确定应加载的图像(-568h@2x-667h@2x)。

有关更多信息和方便的UIImage类别,请参阅此答案https://stackoverflow.com/a/26684508/753878

于 2014-11-14T09:36:43.217 回答