In iPhone App, while running the App on device How to detect the screen resolution of the device on which App is running?
在iPhone应用程序中,运行App时如何检测App运行的设备的屏幕分辨率?
6 个解决方案
#1
330
CGRect screenBounds = [[UIScreen mainScreen] bounds];
That will give you the entire screen's resolution in points, so it would most typically be 320x480 for iPhones. Even though the iPhone4 has a much larger screen size iOS still gives back 320x480 instead of 640x960. This is mostly because of older applications breaking.
这将为你提供以点为单位的整个屏幕分辨率,因此iphone的分辨率通常为320x480。尽管iPhone4的屏幕尺寸比iOS大得多,但iOS仍然会回馈320x480,而不是640x960。这主要是因为旧的应用程序崩溃了。
CGFloat screenScale = [[UIScreen mainScreen] scale];
This will give you the scale of the screen. For all devices that do not have Retina Displays this will return a 1.0f, while Retina Display devices will give a 2.0f and the iPhone 6 Plus (Retina HD) will give a 3.0f.
这将给出屏幕的大小。对于所有没有视网膜显示屏的设备,这将返回1.0f,而视网膜显示屏设备将给出2.0f, iPhone 6 Plus (Retina HD)将给出3.0f。
Now if you want to get the pixel width & height of the iOS device screen you just need to do one simple thing.
现在,如果你想要得到iOS设备屏幕的像素宽度和高度,你只需要做一件简单的事情。
CGSize screenSize = CGSizeMake(screenBounds.size.width * screenScale, screenBounds.size.height * screenScale);
By multiplying by the screen's scale you get the actual pixel resolution.
通过乘以屏幕的比例,你得到了实际的像素分辨率。
A good read on the difference between points and pixels in iOS can be read here.
可以在这里阅读关于iOS中的点和像素之间的区别的好书。
EDIT: (Version for Swift)
编辑:斯威夫特(版本)
let screenBounds = UIScreen.main.bounds
let screenScale = UIScreen.main.scale
let screenSize = CGSize(width: screenBounds.size.width * screenScale, height: screenBounds.size.height * screenScale)
#2
9
UIScreen class lets you find screen resolution in Points and Pixels.
UIScreen类允许您以点和像素为单位查找屏幕分辨率。
Screen resolutions is measured in Points or Pixels. It should never be confused with screen size. A smaller screen size can have higher resolution.
屏幕分辨率是以点或像素来度量的。它不应该与屏幕大小混淆。屏幕尺寸越小,分辨率越高。
UIScreen's 'bounds.width' return rectangular size in Points
UIScreen的界限。宽度以点为单位返回矩形尺寸
UIScreen's 'nativeBounds.width' return rectangular size in Pixels.This value is detected as PPI ( Point per inch ). Shows the sharpness & clarity of the Image on a device.
UIScreen的nativeBounds。宽度'返回矩形大小以像素为单位。这个值被检测为PPI(每英寸点)。显示设备上图像的清晰度和清晰度。
You can use UIScreen class to detect all these values.
您可以使用UIScreen类来检测所有这些值。
Swift3
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)")
Console
控制台
width:736.0
height:414.0
Native Width:1080.0
Native Height:1920.0
Swift 2.x
快2.倍
//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
ObjectiveC
ObjectiveC
// 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
#3
8
Use it in App Delegate: I am using storyboard
在App委托中使用:我正在使用故事板
- (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;
}
#4
4
See the UIScreen Reference: http://developer.apple.com/library/ios/#documentation/uikit/reference/UIScreen_Class/Reference/UIScreen.html
参见UIScreen引用:http://developer.apple.com/library/ios/#文档/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");
}
#5
4
For iOS 8 we can just use this [UIScreen mainScreen].nativeBounds
, like that:
对于iOS 8,我们可以使用这个[UIScreen主屏幕]。nativeBounds,像这样:
- (NSInteger)resolutionX
{
return CGRectGetWidth([UIScreen mainScreen].nativeBounds);
}
- (NSInteger)resolutionY
{
return CGRectGetHeight([UIScreen mainScreen].nativeBounds);
}
#6
0
Use this code it will help for getting any type of device's screen resolution
使用此代码将有助于获得任何类型的设备的屏幕分辨率
[[UIScreen mainScreen] bounds].size.height
[[UIScreen mainScreen] bounds].size.width
#1
330
CGRect screenBounds = [[UIScreen mainScreen] bounds];
That will give you the entire screen's resolution in points, so it would most typically be 320x480 for iPhones. Even though the iPhone4 has a much larger screen size iOS still gives back 320x480 instead of 640x960. This is mostly because of older applications breaking.
这将为你提供以点为单位的整个屏幕分辨率,因此iphone的分辨率通常为320x480。尽管iPhone4的屏幕尺寸比iOS大得多,但iOS仍然会回馈320x480,而不是640x960。这主要是因为旧的应用程序崩溃了。
CGFloat screenScale = [[UIScreen mainScreen] scale];
This will give you the scale of the screen. For all devices that do not have Retina Displays this will return a 1.0f, while Retina Display devices will give a 2.0f and the iPhone 6 Plus (Retina HD) will give a 3.0f.
这将给出屏幕的大小。对于所有没有视网膜显示屏的设备,这将返回1.0f,而视网膜显示屏设备将给出2.0f, iPhone 6 Plus (Retina HD)将给出3.0f。
Now if you want to get the pixel width & height of the iOS device screen you just need to do one simple thing.
现在,如果你想要得到iOS设备屏幕的像素宽度和高度,你只需要做一件简单的事情。
CGSize screenSize = CGSizeMake(screenBounds.size.width * screenScale, screenBounds.size.height * screenScale);
By multiplying by the screen's scale you get the actual pixel resolution.
通过乘以屏幕的比例,你得到了实际的像素分辨率。
A good read on the difference between points and pixels in iOS can be read here.
可以在这里阅读关于iOS中的点和像素之间的区别的好书。
EDIT: (Version for Swift)
编辑:斯威夫特(版本)
let screenBounds = UIScreen.main.bounds
let screenScale = UIScreen.main.scale
let screenSize = CGSize(width: screenBounds.size.width * screenScale, height: screenBounds.size.height * screenScale)
#2
9
UIScreen class lets you find screen resolution in Points and Pixels.
UIScreen类允许您以点和像素为单位查找屏幕分辨率。
Screen resolutions is measured in Points or Pixels. It should never be confused with screen size. A smaller screen size can have higher resolution.
屏幕分辨率是以点或像素来度量的。它不应该与屏幕大小混淆。屏幕尺寸越小,分辨率越高。
UIScreen's 'bounds.width' return rectangular size in Points
UIScreen的界限。宽度以点为单位返回矩形尺寸
UIScreen's 'nativeBounds.width' return rectangular size in Pixels.This value is detected as PPI ( Point per inch ). Shows the sharpness & clarity of the Image on a device.
UIScreen的nativeBounds。宽度'返回矩形大小以像素为单位。这个值被检测为PPI(每英寸点)。显示设备上图像的清晰度和清晰度。
You can use UIScreen class to detect all these values.
您可以使用UIScreen类来检测所有这些值。
Swift3
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)")
Console
控制台
width:736.0
height:414.0
Native Width:1080.0
Native Height:1920.0
Swift 2.x
快2.倍
//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
ObjectiveC
ObjectiveC
// 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
#3
8
Use it in App Delegate: I am using storyboard
在App委托中使用:我正在使用故事板
- (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;
}
#4
4
See the UIScreen Reference: http://developer.apple.com/library/ios/#documentation/uikit/reference/UIScreen_Class/Reference/UIScreen.html
参见UIScreen引用:http://developer.apple.com/library/ios/#文档/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");
}
#5
4
For iOS 8 we can just use this [UIScreen mainScreen].nativeBounds
, like that:
对于iOS 8,我们可以使用这个[UIScreen主屏幕]。nativeBounds,像这样:
- (NSInteger)resolutionX
{
return CGRectGetWidth([UIScreen mainScreen].nativeBounds);
}
- (NSInteger)resolutionY
{
return CGRectGetHeight([UIScreen mainScreen].nativeBounds);
}
#6
0
Use this code it will help for getting any type of device's screen resolution
使用此代码将有助于获得任何类型的设备的屏幕分辨率
[[UIScreen mainScreen] bounds].size.height
[[UIScreen mainScreen] bounds].size.width