Objective c 如何在mac app中获得屏幕分辨率的纵横比
我试图得到屏幕分辨率的纵横比,下面是我的代码,从中我可以得到宽度高度和刷新率Objective c 如何在mac app中获得屏幕分辨率的纵横比,objective-c,macos,aspect-ratio,Objective C,Macos,Aspect Ratio,我试图得到屏幕分辨率的纵横比,下面是我的代码,从中我可以得到宽度高度和刷新率 -(void)getSupportedDisplays{ NSArray* theref = (__bridge NSArray *)(CGDisplayCopyAllDisplayModes ( CGMainDisplayID(), nil )); NSMutableArray * rezes = [[NSMutableArray alloc]init]; for (id aMod
-(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);
}
这将返回最大的共同因素
int commanDivideFactor = gcd(theWidth, theHeight);
NSLog(@"%d : %d", theWidth/commanDivideFactor, theHeight/commanDivideFactor);