How to read the physical screen size of OSX?

前端 未结 2 1965
长发绾君心
长发绾君心 2020-12-01 06:42

I would like to know the physical screen size under Mac OSX. But NSDeviceResolution is always reporting wrong value (72), so the calculation result of resolutio

2条回答
  •  长情又很酷
    2020-12-01 07:36

    You can use CGDisplayScreenSize to get the physical size of a screen in millimetres. From that you can compute the DPI given that you already know the resolution.

    So e.g.

    NSScreen *screen = [NSScreen mainScreen];
    NSDictionary *description = [screen deviceDescription];
    NSSize displayPixelSize = [[description objectForKey:NSDeviceSize] sizeValue];
    CGSize displayPhysicalSize = CGDisplayScreenSize(
                [[description objectForKey:@"NSScreenNumber"] unsignedIntValue]);
    
    NSLog(@"DPI is %0.2f", 
             (displayPixelSize.width / displayPhysicalSize.width) * 25.4f); 
             // there being 25.4 mm in an inch
    

    That @"NSScreenNumber" thing looks dodgy but is the explicit documented means of obtaining a CGDirectDisplayID from an NSScreen.

提交回复
热议问题