如何在Mac App中获取屏幕分辨率的长宽比

时间:2019-04-08 09:45:16

标签: objective-c macos aspect-ratio

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

-(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);


}

我希望每种分辨率的长宽比都是这样

like this

有什么建议吗?

预先感谢!

1 个答案:

答案 0 :(得分:1)

您可以从宽度和高度中获得宽高比,您所需要做的就是找到“宽度和高度”的最大公因数。

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

这将返回最大公因数

  int commanDivideFactor = gcd(theWidth, theHeight);

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