iOS iPhone物理屏幕大小(毫米)



有没有办法在iOS中获得以毫米为单位的iPhone物理屏幕大小?

唯一可用的信息是每款iPhone的PPI和屏幕大小(像素而非毫米(。

这是Swift CGSize扩展,允许检索当前设备的iPhone物理屏幕大小(以毫米为单位(:

注意:它只支持iPhone X和旧型号。

extension CGSize {
/**
* Physical device screen size in millimeters
*/
static func physicalDeviceScreenSize() -> CGSize? {
guard let ppi = deviceModelNameToPpiMapping[UIDevice().modelName], ppi != 0 else {
return nil
}
let width = (UIScreen.main.nativeBounds.width * 25.4) / ppi
let height = (UIScreen.main.nativeBounds.height * 25.4) / ppi
return CGSize(width: width, height: height)
}
static private var deviceModelNameToPpiMapping: Dictionary<String, CGFloat> {
// PPI values derived from https://www.paintcodeapp.com/news/ultimate-guide-to-iphone-resolutions
return [
"iPhone10,3": 458.0, // iPhone X
"iPhone10,6": 458.0, // iPhone X
"iPhone11,2": 458.0, // iPhone Xs
"iPhone11,4": 458.0, // iPhone Xs Max
"iPhone11,6": 458.0, // iPhone Xs Max
"iPhone11,8": 326.0, // iPhone Xr
]
}
}

最新更新