iOS SDK是否提供了一种简单的方法来检查currentDevice是否具有高分辨率的显示器(retina) ?
我现在发现的最好的方法是:
if ([[UIScreen mainScreen] respondsToSelector:@selector(scale)] == YES && [[UIScreen mainScreen] scale] == 2.00) {
// RETINA DISPLAY
}
iOS SDK是否提供了一种简单的方法来检查currentDevice是否具有高分辨率的显示器(retina) ?
我现在发现的最好的方法是:
if ([[UIScreen mainScreen] respondsToSelector:@selector(scale)] == YES && [[UIScreen mainScreen] scale] == 2.00) {
// RETINA DISPLAY
}
当前回答
@sickp的答案是正确的。为了简化操作,将这一行添加到共享中。pch文件:
#define IS_RETINA ([[UIScreen mainScreen] respondsToSelector:@selector(displayLinkWithTarget:selector:)] && ([UIScreen mainScreen].scale >= 2.0))
然后在任何文件中你都可以这样做:
if(IS_RETINA)
{
// etc..
}
其他回答
// .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;
}
这是对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
SSToolkit有一个这样做的方法:
http://sstoolk.it/documentation/Categories/UIScreen (SSToolkitAdditions) . html
它的用法如下:
[[UIScreen mainScreen] isRetinaDisplay];
这个代码片段……
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;
}
将返回…… 0为标准分辨率iPhone/iPod touch, 1为retina iPhone, 2用于标准分辨率的iPad, 3视网膜iPad。
+(BOOL)iPhoneRetina{
return ([[UIScreen mainScreen] respondsToSelector:@selector(displayLinkWithTarget:selector:)] && ([UIScreen mainScreen].scale == 2.0))?1:0;
}