Getting the screen resolution in iphone

时光毁灭记忆、已成空白 提交于 2020-01-05 07:52:06

问题


Hi I need to get the screen width and height. I have used this way

UIWindow* window = [UIApplication sharedApplication].keyWindow;
NSLog(@"%f",window.frame.size.width);
NSLog(@"%f",window.frame.size.height);

The problem is I have an iPhone 4 but the resolution is shown as 480x320 which is incorrect...


回答1:


float scaleFactor = [[UIScreen mainScreen] scale];
CGRect screen = [[UIScreen mainScreen] bounds];
CGFloat widthInPixel = screen.size.width * scaleFactor;
CGFloat heightInPixel = screen.size.height * scaleFactor;

About the screen resolution of 480x320, it is absolutely right as this gives you the logical size or you can say size in points as 480 points x 320 points.

There is difference between a point and pixel. Pixel is the smallest unit of display.

Where as Point is the collection of pixel.

In case of iPhone 4 1 point = 2 pixel
In case of iPhone 3GS 1 point = 1 pixel

Please refer docs for scale and bounds.

bounds : Contains the bounding rectangle of the screen, measured in points.
scale : This value reflects the scale factor needed to convert from the default logical coordinate space into the device coordinate space of this screen. The default logical coordinate space is measured using points, where one point is approximately equal to 1/160th of an inch. If a device’s screen has a reasonably similar pixel density, the scale factor is typically set to 1.0 so that one point maps to one pixel. However, a screen with a significantly different pixel density may set this property to a higher value.




回答2:


You have to multiply by the scale of the screen:

CGSize sz = [UIScreen mainScreen].bounds.size;
float sc = [UIScreen mainScreen].scale;
size_t w = sz.width * sc;
size_t h = sz.height * sc;



回答3:


You need to get the scale of the screen and multiply it by the width and height. E.g.:

UIWindow* window = [UIApplication sharedApplication].keyWindow;
UIScreen *screen = window.screen;
CGFloat scale = screen.scale;

NSLog(@"%f",window.frame.size.width);
NSLog(@"%f",window.frame.size.height);
NSLog(@"screen pixels width: %f height: %f", window.frame.size.width * scale, window.frame.size.height * scale);



回答4:


If you need the screen height and width use this :

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



回答5:


CGRect screen = [[UIScreen mainScreen] bounds];
CGFloat width = CGRectGetWidth(screen);
CGFloat height = CGRectGetHeight(screen);



回答6:


what you need is just this : [UIScreen mainScreen].nativeBounds ,the API from iOS 8




回答7:


By this code you can get screen height and width of any iphone and use it in your application as you wish.

 [[UIScreen mainScreen] bounds].size.height
 [[UIScreen mainScreen] bounds].size.width


来源:https://stackoverflow.com/questions/14003955/getting-the-screen-resolution-in-iphone

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!