簡體   English   中英

iOS以編程方式獲取物理屏幕大小?

[英]iOS get physical screen size programmatically?

這可能嗎? 我想要英寸數,而不是像素數。 我知道它大約是 160 ppi。 但不完全是。

沒有一個 API 會給你這個。 最好的辦法是查看設備的屏幕尺寸(以磅為單位),並根據它是 iPad 還是 iPhone 等進行推測,然后對屏幕尺寸使用硬編碼值。

這是一些獲取屏幕大小的代碼:

CGRect screenRect = [[UIScreen mainScreen] bounds];
CGFloat screenWidth = screenRect.size.width;
CGFloat screenHeight = screenRect.size.height;

請注意,寬度和高度可能會交換,具體取決於設備方向。

如果它可用,它將在UIScreenUIDevice 中,但它不在那里。

您可以從Erica 的 UIDevice-extension 中的信息Wikipedia 上列出的每個設備的規格中推斷出它。

這是估計設備屏幕大小的簡短方法。 它針對最新的設備進行了更新,但在未來的設備上可能會失敗(就像所有猜測方法一樣)。 如果設備被鏡像(返回設備的屏幕尺寸,而不是鏡像屏幕尺寸),它也會變得混亂

#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
    }
} 

您可能需要使用[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;

既然有人問了這個問題,我就創建了一個開源庫來處理這個問題: IRLSize 它可以用於任一方向:以現實世界的維度測量視圖(或整個屏幕)的大小,或將視圖的大小設置為特定的現實世界維度。

也許 Jeff Hay 的代碼可以改編為包含 iPad Mini。 訣竅是獲取設備的型號標識符。 最新的非視網膜 iPad 是“iPad2,4”,第一款 iPad mini 是“iPad2,5”。 現在您需要檢查的是屏幕縮放比例是否為 1.0(非視網膜)

盡管此代碼不是面向未來的,但您始終可以為模型標識符添加更多規則。

#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

我使用的“公式”是

#define IS_iPhone5 ( fabs( (double)[ [ UIScreen mainScreen ] bounds ].size.height - (double)568 ) < DBL_EPSILON )

注意:屏幕旋轉在這里很重要

extension UIScreen {
    var physicalSize:CGSize {
        return CGSize(width: bounds.width*scale, height: bounds.height*scale)
    }
}

使用:

print(UIScreen.main.physicalSize)

這是獲取屏幕尺寸的 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
}

這些作為標准功能包含在:

https://github.com/goktugyil/EZSwiftExtensions

沒有人提到fixedCoordinateSpace 在 Swift 3 中要以縱向向上的方向獲取屏幕尺寸,您應該使用: UIScreen.main.fixedCoordinateSpace.bounds

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);

  • 規模是存在的!

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM