我正在尝试获取以下屏幕分辨率的宽高比,这是我从中获取宽度,高度和刷新率的代码
-(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);
}
我希望每种分辨率的长宽比都是这样
有什么建议吗?
预先感谢!
答案 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);