如何以编程方式精确地找到PPI

如何以编程方式精确地找到PPI

本文介绍了如何以编程方式精确地找到PPI的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在iOS中找出PPI(每英寸像素).

I am trying to find out PPI(Pixels Per Inch) in iOS.

我找不到像查询显示尺寸一样直接查询的方法

I couldn't find any direct way to query this like we do for display size

UIScreen.mainScreen().bounds

通过将比例乘以适用于iPhone(163)或iPad(132)的标准通用PPI,有一种方式 ),但不准确.

There is a way to do it by multiplying scale with standard generic PPI for iPhone(163) or iPad(132) but it's not accurate.

如果公式正确,则 iPhone 6 plus的 PPI 为489 ,但实际上 PPI为401 ,这是参考

If the formula is right then PPI of iPhone 6 plus is 489 but in reality the PPI is 401 Here is the reference

目前看来,硬编码似乎是要走的路.

For now it seems like hardcoding is the way to go.

但是我想使用公式以编程方式进行操作.

But I'd like to do it programmatically using a formula.

推荐答案

我刚刚将一个旧的ObjC库移植并更新到了Swift.您可以使用它或获取所需代码的一部分.在此处获取它: https://github.com/marchv/UIScreenExtension .

I have just ported and updated one of my old ObjC libraries to Swift. You can use it or take parts of the code you need. Get it here: https://github.com/marchv/UIScreenExtension.

该库使用UIScreen.main.nativeScale将每英寸的像素(PPI)转换为每英寸的点数.

The library uses UIScreen.main.nativeScale to convert from Pixels Per Inch (PPI) to Points Per Inch.

使用Cocoapods安装该库,然后将其导入:

Install the library using Cocoapods and then import it:

import UIScreenExtension

然后利用它:

if let pointsPerCentimeter = UIScreen.pointsPerCentimeter {
   // code
}

这篇关于如何以编程方式精确地找到PPI的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-29 12:30