Search code examples
iphoneobjective-ciosscreen-resolution

Getting the screen resolution in iphone


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...


Solution

  • 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.