Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to find PPI programmatically with precision

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

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.

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.

like image 970
Durai Amuthan.H Avatar asked Apr 14 '16 09:04

Durai Amuthan.H


1 Answers

iPhone Plus has scale 3, but nativeScale is 2.6.

UIKit samples this content down to fit the actual screen dimensions. Metal or OpenGL ES contents should be rendered at the precise dimensions.

int screenPPI() {
    return [[UIScreen mainScreen] nativeScale] * ((UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) ? 132 : 163);
}
like image 139
Evgeny Karpov Avatar answered Oct 16 '22 12:10

Evgeny Karpov