2011-01-24 81 views

回答

328
CGRect screenBounds = [[UIScreen mainScreen] bounds]; 

这会给你整个屏幕的分辨率分,所以这将最典型的为320x480像素的iPhone。尽管iPhone4拥有更大的屏幕尺寸,但iOS仍然支持320x480而不是640x960。这主要是因为较旧的应用程序中断。

CGFloat screenScale = [[UIScreen mainScreen] scale]; 

这会给你屏幕的比例。对于没有视网膜显示器的所有设备,这将返回1.0f,而Retina显示设备将提供2.0f,iPhone 6 Plus(Retina HD)将提供3.0f。

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

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

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

关于iOS中点和像素之间的区别,可以阅读here

编辑:(版本为SWIFT)

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

见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"); 
} 
+0

thanx的回应,如果我在的NSLog把它(@ “%d”,[UIScreen mainScreen] scale]);它给出0 ......和NSLog(@“%@”,[[UIScreen mainScreen] scale]);它给nil请让我知道如何获得屏幕分辨率或如何测试在模拟器上运行它时是否给出正确的分辨率 – ios 2011-01-24 07:22:45

+2

尝试`NSLog(@“%f”,[[UIScreen mainScreen] scale]);` – vikingosegundo 2011-01-24 12:12:50

+0

@ vikingosegundo,谢谢 – ios 2011-01-25 03:34:02

8

使用它在应用代表:我用故事板

- (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]; 

     [email protected]"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); 
     [email protected]"5"; 
    } 

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

} 

return YES; 
} 
4

对于iOS 8,我们可以只使用这个[UIScreen mainScreen].nativeBounds,这样的:

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

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

UIScreen类让你找到屏幕点和像素的解决方案。

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

UIScreen在积分enter image description here

UIScreen的“nativeBounds.width”返回的矩形尺寸在Pixels.This值“bounds.width”返回的矩形尺寸被检测为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 

夫特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 

的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 
0

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

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