ios 检测视网膜显示屏

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3504173/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-30 17:33:50  来源:igfitidea点击:

Detect Retina Display

iosiphoneretina-displayuiscreen

提问by Pierre Valade

Does iOS SDK provides an easy way to check if the currentDevice has an high-resolution display (retina) ?

iOS SDK 是否提供了一种简单的方法来检查 currentDevice 是否具有高分辨率显示器(视网膜)?

The best way I've found to do it now is :

我现在找到的最好的方法是:

    if ([[UIScreen mainScreen] respondsToSelector:@selector(scale)] == YES && [[UIScreen mainScreen] scale] == 2.00) {
         // RETINA DISPLAY
    }

回答by sickp

In order to detect the Retina display reliably on all iOS devices, you need to check if the device is running iOS4+ and if the [UIScreen mainScreen].scaleproperty is equal to 2.0. You CANNOT assume a device is running iOS4+ if the scaleproperty exists, as the iPad 3.2 also contains this property.

为了在所有 iOS 设备上可靠地检测 Retina 显示,您需要检查设备是否运行 iOS4+ 以及该[UIScreen mainScreen].scale属性是否等于 2.0。如果该scale属性存在,您不能假设设备正在运行 iOS4+ ,因为 iPad 3.2 也包含此属性。

On an iPad running iOS3.2, scale will return 1.0 in 1x mode, and 2.0 in 2x mode -- even though we know that device does not contain a Retina display. Apple changed this behavior in iOS4.2 for the iPad: it returns 1.0 in both 1x and 2x modes. You can test this yourself in the simulator.

在运行 iOS3.2 的 iPad 上,缩放将在 1x 模式下返回 1.0,在 2x 模式下返回 2.0——即使我们知道该设备不包含 Retina 显示屏。Apple 在 iOS4.2 中为 iPad 更改了这种行为:它在 1x 和 2x 模式下都返回 1.0。您可以在模拟器中自行测试。

I test for the -displayLinkWithTarget:selector:method on the main screen which exists in iOS4.x but not iOS3.2, and then check the screen's scale:

-displayLinkWithTarget:selector:在主屏幕上测试iOS4.x中存在但iOS3.2中不存在的方法,然后检查屏幕的比例:

if ([[UIScreen mainScreen] respondsToSelector:@selector(displayLinkWithTarget:selector:)] &&
    ([UIScreen mainScreen].scale == 2.0)) {
  // Retina display
} else {
  // non-Retina display
}

回答by Mick Byrne

@sickp's answer is correct. Just to make things easier, add this line into your Shared.pch file:

@sickp 的回答是正确的。为方便起见,将此行添加到您的 Shared.pch 文件中:

#define IS_RETINA ([[UIScreen mainScreen] respondsToSelector:@selector(displayLinkWithTarget:selector:)] && ([UIScreen mainScreen].scale >= 2.0))

Then in any file you can just do:

然后在任何文件中,您都可以执行以下操作:

if(IS_RETINA)
{
   // etc..
}

回答by Mani

+(BOOL)iPhoneRetina{
    return ([[UIScreen mainScreen] respondsToSelector:@selector(displayLinkWithTarget:selector:)] && ([UIScreen mainScreen].scale == 2.0))?1:0;
}

回答by primulaveris

Here is a handy swift extension:

这是一个方便的 swift 扩展:

Update for Swift v5:

Swift v5 更新:

extension UIScreen {

    public var isRetina: Bool {
        guard let scale = screenScale else {
            return false
        }
        return scale >= 2.0
    }

    public var isRetinaHD: Bool {
        guard let scale = screenScale else {
            return false
        }
        return scale >= 3.0
    }

    private var screenScale: CGFloat? {
        guard UIScreen.main.responds(to: #selector(getter: scale)) else {
            return nil
        }
        return UIScreen.main.scale
    }
}

Usage:

用法:

if UIScreen.main.isRetina {
    // Your code
}

Original:

原来的:

extension UIScreen { 
public func isRetina() -> Bool {
    return screenScale() >= 2.0
}

public func isRetinaHD() -> Bool {
    return screenScale() >= 3.0
}

private func screenScale() -> CGFloat? {
    if UIScreen.mainScreen().respondsToSelector(Selector("scale")) {
        return UIScreen.mainScreen().scale
    }
    return nil
    }
}

Usage:

用法:

if UIScreen.mainScreen().isRetina() {
 // your code
        }

回答by Pedro

This snippet...

这个片段...

    int d = 0; // standard display
if ([[UIScreen mainScreen] respondsToSelector:@selector(scale)] && [[UIScreen mainScreen] scale] == 2.0) {
    d = 1; // is retina display
}

if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPad) {
    d += 2;
}

Will return... 0 for standard resolution iPhone/iPod touch, 1 for retina iPhone, 2 for standard resolution iPad, 3 for retina iPad.

将返回... 0 代表标准分辨率 iPhone/iPod touch,1 代表视网膜 iPhone,2 代表标准分辨率 iPad,3 代表视网膜 iPad。

回答by Jorge Perez

SSToolkit has a method that does this:

SSToolkit 有一个方法可以做到这一点:

http://sstoolk.it/documentation/Categories/UIScreen(SSToolkitAdditions).html

http://sstoolk.it/documentation/Categories/UIScreen(SSToolkitAdditions).html

It is used in the following way:

它的使用方式如下:

[[UIScreen mainScreen] isRetinaDisplay];

回答by skahlert

It always feels a bit dodgy to compare floating-point values for equality. I prefer going for either

比较浮点值的相等性总是感觉有点狡猾。我更喜欢去

[UIScreen mainScreen].scale > 1.0;

or

或者

[UIScreen mainScreen].scale < 2.0;

回答by Dan Rosenstark

This is a riff on Matt MC's answer above. Just a category on UIScreen.

这是对上述 Matt MC 回答的即兴演奏。只是一个类别UIScreen

#import "UIScreen+Util.h"

@implementation UIScreen (Util)

+ (BOOL) isRetinaDisplay {
    static BOOL retina = NO;
    static BOOL alreadyChecked = NO;
    if (!alreadyChecked) {
        UIScreen *mainScreen = self.mainScreen;
        if (mainScreen) {
            retina = mainScreen.scale > 1.0;
            alreadyChecked = YES;
        }
    }
    return retina;
}

@end

回答by cdf1982

Swift version of the answers above, with >= 2.0 scale so it includes iPhone 6+ and other future devices with higher-than-Retina scale:

上面答案的 Swift 版本,具有 >= 2.0 比例,因此它包括 iPhone 6+ 和其他具有高于 Retina 比例的未来设备:

 if UIScreen.mainScreen().respondsToSelector(Selector("scale")) && UIScreen.mainScreen().scale >= 2.0 {
    // code executed only on Retina device
}

回答by Roman Solodyashkin

// .h
UIKIT_EXTERN bool isRetinaDisplay();

// .m
bool isRetinaDisplay()
{
    static bool flag;
#ifdef __BLOCKS__
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        if([[UIScreen mainScreen] respondsToSelector:@selector(scale)])
        {
            flag = [[UIScreen mainScreen] scale] > 1.0;
        }
        else
        {
            flag = false;
        }
    });
#else
    static bool onceToken;
    if(onceToken == false)
    {
        onceToken = true;
        if([[UIScreen mainScreen] respondsToSelector:@selector(scale)])
        {
            flag = [[UIScreen mainScreen] scale] > 1.0;
        }
        else
        {
            flag = false;
        }
    }
#endif
    return flag;
}