在iPhone应用中如何检测设备的屏幕分辨率

139

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

7个回答

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

这将给你整个屏幕的分辨率,以点为单位,因此iPhone最常见的分辨率是320x480。即使iPhone4有更大的屏幕尺寸,iOS仍然返回320x480而不是640x960。这主要是因为旧应用程序会出现问题。

CGFloat screenScale = [[UIScreen mainScreen] scale];

这将给您屏幕的比例。对于所有没有Retina显示屏的设备,这将返回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中点和像素的区别的好读物可以在此处阅读。

编辑:(适用于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
除了iPhone 6 Plus这种情况,这并不是“实际像素”分辨率。这是所有代码中呈现的分辨率(除了OpenGL),采用3倍比例尺进行渲染,但然后在内部对其进行下采样以适应屏幕的本地分辨率为1080 x 1920。在链接中有许多好的解释。 - RobP
遗憾的是,这不会给你屏幕元素的“真实”尺寸,因为苹果对“点”和“比例”的概念只是近似值(请参阅 iPhone vs iPad vs iPad mini 的规格)。可能是为了减少存在的不同组合数量。我认为 iPhone 6 Plus 特别偏离。 - ToolmakerSteve
实际上,6+并不太远:高度736个点/160(点/英寸)= 4.60英寸逻辑高度;实际屏幕高度为4.79英寸;误差为5%。iPad的误差要大得多:高度1024个点/160(点/英寸)= 6.40英寸逻辑高度;实际屏幕高度为7.76英寸;误差为20%。iPad mini则没有问题;它与原始iPhone的密度相匹配。对于大多数情况而言,这意味着应该在iPad mini上测试iPad软件(以确保可用性),然后简单地忽略大多数iPad将图像放大20%的事实(与iPhone或iPad mini相比)。 - ToolmakerSteve
1
@RobP 那么你如何解决 iPhone 6 Plus 的这个问题? - Crashalot
1
@Crashalot 不确定你所说的“解决这个问题”是什么意思?这取决于您在获取屏幕分辨率时所考虑的目的。就程序员而言,Jman012的答案是正确的,您可以将其渲染到1242x2208或2208x1242的空间中。事实上,这甚至是我们提供启动图像的分辨率。硬件将此图像进行下采样并在具有较小像素计数的物理屏幕上显示,这将是我们的代码不应该知道的“实现细节”。 - RobP
显示剩余2条评论

29

UIScreen类可让您在点和像素中找到屏幕分辨率。

屏幕分辨率以点或像素测量,不应与屏幕尺寸混淆。尺寸较小的屏幕可以具有更高的分辨率。

UIScreen的“bounds.width”以点为单位返回矩形大小enter image description here

UIScreen的“nativeBounds.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

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

Objective-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

7
在 App Delegate 中使用它:我正在使用故事板 (Storyboard)。
- (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;
 }

5

对于iOS 8,我们可以直接使用[UIScreen mainScreen].nativeBounds来获取屏幕尺寸,示例如下:

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

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

4

谢谢回复。如果我把它放在 NSLog(@"%d",[[UIScreen mainScreen] scale]); 中,它会返回 0......而 NSLog(@"%@",[[UIScreen mainScreen] scale]); 则返回 nil。请告诉我如何获取屏幕分辨率或如何测试在模拟器上运行时是否提供了正确的分辨率。 - ios
2
尝试运行 NSLog(@"%f",[[UIScreen mainScreen] scale]); - vikingosegundo

0
使用此代码可帮助获取任何类型设备的屏幕分辨率。
 [[UIScreen mainScreen] bounds].size.height
 [[UIScreen mainScreen] bounds].size.width

0
如果您的目标是获取模型分辨率类型而不是分辨率值本身,则此Swift解决方案可能会有所帮助:
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
    }

}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接