如何在 mac 应用程序中获取屏幕分辨率的纵横比

How to get aspect ratio of screen resolutions in mac app

我正在尝试获取屏幕分辨率的纵横比,下面是我的代码,我从中获取宽度高度和刷新率

-(void)getSupportedDisplays{

    NSArray* theref  =  (__bridge NSArray *)(CGDisplayCopyAllDisplayModes ( CGMainDisplayID(), nil ));

    NSMutableArray * rezes = [[NSMutableArray  alloc]init];

    for (id aMode  in theref) {
        CGDisplayModeRef  thisMode = (__bridge CGDisplayModeRef)(aMode);
        size_t theWidth = CGDisplayModeGetWidth( thisMode );
        size_t theHeight = CGDisplayModeGetHeight( thisMode );
        double refresh = CGDisplayModeGetRefreshRate(thisMode);
        NSString *theRez = [NSString stringWithFormat:@"%zux%zu %d Hz",theWidth,theHeight,(int)refresh];

        if (![rezes containsObject:theRez]) {
            [rezes addObject:theRez];
        }
    }

    NSLog(@" display deatails = %@", rezes);


}

我想要每个分辨率的纵横比是这样的

有什么建议吗?

提前致谢!!!

您可以从宽度和高度获得纵横比,您只需要找到宽度和高度的最大公约数。

static int gcd (int a, int b) {
    return (b == 0) ? a : gcd (b, a%b);
}

这将return最大公约数

  int commanDivideFactor = gcd(theWidth, theHeight);

  NSLog(@"%d : %d", theWidth/commanDivideFactor, theHeight/commanDivideFactor);