ios iOS以编程方式获取物理屏幕大小?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8257947/
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
iOS get physical screen size programmatically?
提问by William Jockusch
Is this possible? I want the number of inches, not the number of pixels. I know it is approximately 160 ppi. But not exactly.
这可能吗?我想要英寸数,而不是像素数。我知道它大约是 160 ppi。但不完全是。
采纳答案by progrmr
If it were available it would be in UIScreenor UIDevicebut it is not there.
如果它可用,它将在UIScreen或UIDevice 中,但它不在那里。
You can infer it from info in Erica's UIDevice-extensionand the specs for each device listed here on Wikipedia.
您可以从Erica 的 UIDevice-extension 中的信息和Wikipedia 上列出的每个设备的规格中推断出它。
回答by occulus
There isn't an API that will give you this. Your best bet is to look at the device's screen size (in points) and from that surmise if it's an iPad or iPhone etc., and then use hard-coded values for the screen sizes.
没有一个 API 会给你这个。最好的办法是查看设备的屏幕尺寸(以磅为单位),并根据它是 iPad 还是 iPhone 等进行推测,然后对屏幕尺寸使用硬编码值。
Here's some code to get the screen size:
这是一些获取屏幕大小的代码:
CGRect screenRect = [[UIScreen mainScreen] bounds];
CGFloat screenWidth = screenRect.size.width;
CGFloat screenHeight = screenRect.size.height;
Be aware that width and height might be swapped, depending on device orientation.
请注意,宽度和高度可能会交换,具体取决于设备方向。
回答by Jeff Hay
Here's a short method that estimates the device screen size. It's updated as to the latest devices, but may fail on future ones (as all methods of guessing might). It will also get confused if the device is being mirrored (returns the device's screen size, not the mirrored screen size)
这是估计设备屏幕大小的简短方法。它针对最新的设备进行了更新,但在未来的设备上可能会失败(就像所有猜测方法一样)。如果设备被镜像(返回设备的屏幕尺寸,而不是镜像屏幕尺寸),它也会变得混乱
#define SCREEN_SIZE_IPHONE_CLASSIC 3.5
#define SCREEN_SIZE_IPHONE_TALL 4.0
#define SCREEN_SIZE_IPAD_CLASSIC 9.7
+ (CGFloat)screenPhysicalSize
{
if(UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone)
{
CGSize result = [[UIScreen mainScreen] bounds].size;
if (result.height < 500)
return SCREEN_SIZE_IPHONE_CLASSIC; // iPhone 4S / 4th Gen iPod Touch or earlier
else
return SCREEN_SIZE_IPHONE_TALL; // iPhone 5
}
else
{
return SCREEN_SIZE_IPAD_CLASSIC; // iPad
}
}
回答by aelam
You might need use [UIScreen mainScreen].scale;
你可能需要使用 [UIScreen mainScreen].scale;
CGFloat scale = [UIScreen mainScreen].scale;
CGRect screenRect = [[UIScreen mainScreen] bounds];
CGFloat physicalWidth = screenRect.size.width * scale;
CGFloat physicalHeight = screenRect.size.height * scale;
回答by Jeff Kelley
Since this question has been asked, I've created an open-source library to handle this problem: IRLSize. It can be used in either direction: to measure the size of a view (or the whole screen) in real-world dimensions, or to setthe size of a view to a specific real-world dimension.
既然有人问了这个问题,我就创建了一个开源库来处理这个问题:IRLSize。它可以用于任一方向:以现实世界的维度测量视图(或整个屏幕)的大小,或将视图的大小设置为特定的现实世界维度。
回答by philtre
Maybe Jeff Hay's code can be adapted to include iPad Mini. The trick is to get the device's model identifier. The most recent non-retina iPad is "iPad2,4" and the first iPad mini is "iPad2,5". Now all you need to check is if the screen scaling is 1.0 (non-retina)
也许 Jeff Hay 的代码可以改编为包含 iPad Mini。诀窍是获取设备的型号标识符。最新的非视网膜 iPad 是“iPad2,4”,第一款 iPad mini 是“iPad2,5”。现在您需要检查的是屏幕缩放比例是否为 1.0(非视网膜)
Although this code is not future-proof, you can always add more rules for model identifiers.
尽管此代码不是面向未来的,但您始终可以为模型标识符添加更多规则。
#import <sys/utsname.h>
#define SCREEN_SIZE_IPAD_MINI 7.9
struct utsname systemInfo;
uname(&systemInfo);
if(UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad && strcasecmp(systemInfo.machine, "iPad2,5") >= 0 [[UIScreen mainScreen] scale] == 1.0)
return SCREEN_SIZE_IPAD_MINI
回答by Tiago Sousa
The "formula" I use is
我使用的“公式”是
#define IS_iPhone5 ( fabs( (double)[ [ UIScreen mainScreen ] bounds ].size.height - (double)568 ) < DBL_EPSILON )
回答by Esqarrouth
Here is a Swift way to get screen sizes:
这是获取屏幕尺寸的 Swift 方法:
var screenWidth: CGFloat {
if UIInterfaceOrientationIsPortrait(screenOrientation) {
return UIScreen.mainScreen().bounds.size.width
} else {
return UIScreen.mainScreen().bounds.size.height
}
}
var screenHeight: CGFloat {
if UIInterfaceOrientationIsPortrait(screenOrientation) {
return UIScreen.mainScreen().bounds.size.height
} else {
return UIScreen.mainScreen().bounds.size.width
}
}
var screenOrientation: UIInterfaceOrientation {
return UIApplication.sharedApplication().statusBarOrientation
}
These are included as a standard function in:
这些作为标准功能包含在:
回答by Ilya Ilin
Nobody said about fixedCoordinateSpace
. In Swift 3 to get the screen dimensions in a portrait-up orientation you should use: UIScreen.main.fixedCoordinateSpace.bounds
没有人说fixedCoordinateSpace
。在 Swift 3 中以纵向向上获取屏幕尺寸,您应该使用:UIScreen.main.fixedCoordinateSpace.bounds
回答by James Hong
CGFloat scale = [UIScreen mainScreen].scale;
CGRect screenRect = [[UIScreen mainScreen] bounds];
CGFloat screenWidth = screenRect.size.width * (scale/100.0f);
CGFloat screenHeight = screenRect.size.height * (scale/100.0f);
CGFloat scale = [UIScreen mainScreen].scale;
CGRect screenRect = [[UIScreen mainScreen] bounds];
CGFloat screenWidth = screenRect.size.width * (scale/100.0f);
CGFloat screenHeight = screenRect.size.height * (scale/100.0f);
- scale is persent!
- 规模是存在的!