如何检测设备的屏幕分辨率

在 iPhone 应用程序中, 在设备上运行应用程序时如何检测运行应用程序的设备的屏幕分辨率?

121843 次浏览

参见 UIScreen 参考文献: http://developer.apple.com/library/ios/#documentation/uikit/reference/UIScreen_Class/Reference/UIScreen.html

if([[UIScreen mainScreen] respondsToSelector:NSSelectorFromString(@"scale")])
{
if ([[UIScreen mainScreen] scale] < 1.1)
NSLog(@"Standard Resolution Device");


if ([[UIScreen mainScreen] scale] > 1.9)
NSLog(@"High Resolution Device");
}
CGRect screenBounds = [[UIScreen mainScreen] bounds];

这将给你整个屏幕的分辨率点,所以它最典型的是320x480的 iPhone。尽管 iPhone4的屏幕尺寸要大得多,但是 iOS 仍然返回320x480,而不是640x960。这主要是因为旧的应用程序中断了。

CGFloat screenScale = [[UIScreen mainScreen] scale];

这会给你屏幕的大小。对于所有没有视网膜显示器的设备,这将返回一个1.0 f,而视网膜显示器设备将给一个2.0 f 和 iPhone 6 Plus (视网膜 HD)将给一个3.0 f。

现在,如果你想得到 iOS 设备屏幕的像素宽度和高度,你只需要做一件简单的事情。

CGSize screenSize = CGSizeMake(screenBounds.size.width * screenScale, screenBounds.size.height * screenScale);

通过乘以屏幕的比例,你可以得到实际的像素分辨率。

对于 iOS 中点和像素之间的差异,可以读取 给你

编辑: (Swift 版本)

let screenBounds = UIScreen.main.bounds
let screenScale = UIScreen.main.scale
let screenSize = CGSize(width: screenBounds.size.width * screenScale, height: screenBounds.size.height * screenScale)

在应用程序委托中使用它: 我正在使用故事板

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions{


if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone) {


CGSize iOSDeviceScreenSize = [[UIScreen mainScreen] bounds].size;


//----------------HERE WE SETUP FOR IPHONE 4/4s/iPod----------------------


if(iOSDeviceScreenSize.height == 480){


UIStoryboard *iPhone35Storyboard = [UIStoryboard storyboardWithName:@"iPhone" bundle:nil];


// Instantiate the initial view controller object from the storyboard
UIViewController *initialViewController = [iPhone35Storyboard instantiateInitialViewController];


// Instantiate a UIWindow object and initialize it with the screen size of the iOS device
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];


// Set the initial view controller to be the root view controller of the window object
self.window.rootViewController  = initialViewController;


// Set the window object to be the key window and show it
[self.window makeKeyAndVisible];


iphone=@"4";


NSLog(@"iPhone 4: %f", iOSDeviceScreenSize.height);


}


//----------------HERE WE SETUP FOR IPHONE 5----------------------


if(iOSDeviceScreenSize.height == 568){


// Instantiate a new storyboard object using the storyboard file named Storyboard_iPhone4
UIStoryboard *iPhone4Storyboard = [UIStoryboard storyboardWithName:@"iPhone5" bundle:nil];


// Instantiate the initial view controller object from the storyboard
UIViewController *initialViewController = [iPhone4Storyboard instantiateInitialViewController];


// Instantiate a UIWindow object and initialize it with the screen size of the iOS device
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];


// Set the initial view controller to be the root view controller of the window object
self.window.rootViewController  = initialViewController;


// Set the window object to be the key window and show it
[self.window makeKeyAndVisible];


NSLog(@"iPhone 5: %f", iOSDeviceScreenSize.height);
iphone=@"5";
}


} else if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) {
// NSLog(@"wqweqe");
storyboard = [UIStoryboard storyboardWithName:@"iPad" bundle:nil];


}


return YES;
}

对于 iOS8,我们可以使用这个 [UIScreen mainScreen].nativeBounds,就像这样:

- (NSInteger)resolutionX
{
return CGRectGetWidth([UIScreen mainScreen].nativeBounds);
}


- (NSInteger)resolutionY
{
return CGRectGetHeight([UIScreen mainScreen].nativeBounds);
}

UIScreen 类允许您以点和像素的形式查找屏幕分辨率。

屏幕分辨率是以点或像素来衡量的。它不应该与屏幕大小混淆。较小的屏幕尺寸可以有较高的分辨率。

UIScreen 的‘ bound s.width’返回以点为单位的矩形大小enter image description here

UIScreen 的“本地边界宽度”返回以像素为单位的矩形大小。此值检测为 PPI (每英寸点数)。显示设备上图像的清晰度和清晰度。 enter image description here

您可以使用 UIScreen 类来检测所有这些值。

Swift3

// Normal Screen Bounds - Detect Screen size in Points.
let width = UIScreen.main.bounds.width
let height = UIScreen.main.bounds.height
print("\n width:\(width) \n height:\(height)")


// Native Bounds - Detect Screen size in Pixels.
let nWidth = UIScreen.main.nativeBounds.width
let nHeight = UIScreen.main.nativeBounds.height
print("\n Native Width:\(nWidth) \n Native Height:\(nHeight)")

控制台

width:736.0
height:414.0


Native Width:1080.0
Native Height:1920.0

Swift 2. x

//Normal Bounds - Detect Screen size in Points.
let width  = UIScreen.mainScreen.bounds.width
let height = UIScreen.mainScreen.bounds.height


// Native Bounds - Detect Screen size in Pixels.
let nWidth  = UIScreen.mainScreen.nativeBounds.width
let nHeight = UIScreen.mainScreen.nativeBounds.height

目标 C

// Normal Bounds - Detect Screen size in Points.
CGFloat *width  = [UIScreen mainScreen].bounds.size.width;
CGFloat *height = [UIScreen mainScreen].bounds.size.height;


// Native Bounds - Detect Screen size in Pixels.
CGFloat *width  = [UIScreen mainScreen].nativeBounds.size.width
CGFloat *height = [UIScreen mainScreen].nativeBounds.size.width

使用此代码将有助于获得任何类型的设备的屏幕分辨率

 [[UIScreen mainScreen] bounds].size.height
[[UIScreen mainScreen] bounds].size.width

如果您的目标是获得模型分辨率类型,而不是分辨率值本身,这个 斯威夫特解决方案可能会有所帮助:

import UIKit


@objc(IphoneModelScreenSize)
public class IphoneModelScreenSize: NSObject {


// MARK: Enums


public enum IphoneModelScreenSize: Int {
case notAnIphone = 0,
twoThreeOrFour = 1,
se = 2,
sixSevenOrEight = 3,
plus = 4,
elevenXorXS = 5,
elevenProMaxOrXsMax = 6
}


// MARK: Class properties


public class func screenSize() -> IphoneModelScreenSize {
let bounds = UIScreen.main.bounds
let screenWidth = bounds.size.width
let screenHeight = bounds.size.height


switch (screenWidth, screenHeight) {
case (320.0, 480.0):
return .twoThreeOrFour
case (320.0, 568.0):
return .se
case (375.0, 667.0):
return .sixSevenOrEight
case (414.0, 736.0):
return .plus
case (375.0, 812.0):
return .elevenXorXS
case (414.0, 896.0):
return .elevenProMaxOrXsMax
default:
return .notAnIphone
}
}


public class func screenSizeStringValue() -> String {
return screenSizeEnumToString(screenSize())
}


// MARK: Private properties


private class func screenSizeEnumToString(_ screenSize: IphoneModelScreenSize) -> String {
var screenSizeAsString: String


switch screenSize {
case .notAnIphone:
screenSizeAsString = "Not an Iphone"
case .twoThreeOrFour:
screenSizeAsString = "2G, 3G, 3GS, 4 or 4s"
case .se:
screenSizeAsString = "5, 5s, 5c or SE"
case .sixSevenOrEight:
screenSizeAsString = "6, 6s, 7 or 8"
case .plus:
screenSizeAsString = "6+, 6s+, 7+ or 8+"
case .elevenXorXS:
screenSizeAsString = "11 Pro, X or Xs"
case .elevenProMaxOrXsMax:
screenSizeAsString = "11, Xr, 11 Pro Max or Xs Max"
}


return screenSizeAsString
}


}