Reputation: 27211
I've looked a lot of post about iOS how to retrieve the real size. I found such solution but I think it doesn't work correctly for all devices.
+(float) getRealSize {
float scale = 1;
if ([[UIScreen mainScreen] respondsToSelector:@selector(scale)]) {
scale = [[UIScreen mainScreen] scale];
}
float dpi;
if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) {
dpi = 132 * scale;
} else if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone) {
dpi = 163 * scale;
} else {
dpi = 160 * scale;
}
CGRect originalFrame = [[UIScreen mainScreen] bounds];
float w = originalFrame.size.width;
float h = originalFrame.size.height;
return dpi * sqrtf(w*w + h*h);
}
How to update such method to support all devices? I want DPI (PPI) or inches. Or, may be, is there any library to help.
To prevent closing as dublicated: there is no proper solutions in stackoverflow. For, instance, iOS get physical screen size programmatically? Getting the physical screen size in inches for iPhone How to get the screen width and height in iOS? etc.,etc.,etc... no solution now.
Upvotes: 1
Views: 2133
Reputation: 52530
There is no API. All you can do is lookup which device you’re running on, and have a table of physical screen sizes in your app. You need to update for new devices all the time. You may find a library for this.
In addition: if you try to take the size of a pixel from a table, check whether your device is in zoom mode - for example a 7+ in zoom mode will have the same number of pixels as a 7, but with a larger screen. That also means you can’t rely on the screen size in pixels. So any method using the number of pixels to determine the physical screen size will break in zoom mode.
That said, for newer devices that your code doesn’t know, a guess based on number of pixels is the best you can do.
And if your app supports external screens, for anything displayed on another screen you can’t know anything about the screen size.
Upvotes: 0
Reputation: 27211
Resultant code of my question and @lou-franco's answer:
//method
-(float) getRealSize {
float scale = 1.f;
if ([[UIScreen mainScreen] respondsToSelector:@selector(scale)]) {
scale = [[UIScreen mainScreen] scale];
}
float ppi = [GBDeviceInfo deviceInfo].displayInfo.pixelsPerInch;
if (ppi == 0) {
ppi = 160.f;
}
CGRect originalFrame = [[UIScreen mainScreen] bounds];
float w = originalFrame.size.width;
float h = originalFrame.size.height;
return sqrtf(w*w + h*h) * scale / ppi;
}
//usage:
float inches = [self getRealSize];
Upvotes: 0
Reputation: 89142
There is no API in iOS to get this. Use this and make sure to update when there are new devices: https://github.com/lmirosevic/GBDeviceInfo
I don't think this would be a problem for the AppStore, but maybe don't include the jailbreak pod.
Also, you could just copy the info out of: https://github.com/lmirosevic/GBDeviceInfo/blob/master/GBDeviceInfo/GBDeviceInfo_iOS.m -- it's just a list of info about the device -- nothing crazy
Upvotes: 3