如何获得设备比例因子?

时间:2012-12-24 07:16:24

标签: objective-c ios uiimage

我一直在使用UIGraphicsBeginImageContextWithOptions(),我注意到该方法中的最后一个字段是scalefactor。苹果文档说,如果我想将其设置为0.0,那么它将使用设备主屏幕的默认比例因子。有没有办法通过属性或方法获得默认比例因子?

5 个答案:

答案 0 :(得分:101)

设备的比例因子是UIScreen的属性。您可以通过以下方式获取它:

[UIScreen mainScreen].scale;

Documentation here

答案 1 :(得分:23)

Swift 3+版本:

UIScreen.main.scale

答案 2 :(得分:1)

Swift 5+

let scale = UIScreen.main.scale
print("scale is:\(scale)")

答案 3 :(得分:1)

Swift5+ / macOS Big Sur:

let backingScaleFactor = NSScreen.main?.backingScaleFactor

答案 4 :(得分:-1)

希望它能帮到你

 CGSize textcontent =CGSizeMake(width, height);
UIGraphicsBeginImageContext(textcontent);
if ([UIScreen instancesRespondToSelector:@selector(scale)] && [[UIScreen mainScreen] scale] == 2.0f)
{
    UIGraphicsBeginImageContextWithOptions(textcontent, YES, 1.0f);
}
else
{
    UIGraphicsBeginImageContext(textcontent);
}
 [view.layer renderInContext:UIGraphicsGetCurrentContext()];
 viewImage = UIGraphicsGetImageFromCurrentImageContext();
 UIGraphicsEndImageContext();
 return viewImage;