ios 如何获得设备的比例因子?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14018305/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to get the devices scale factor?
提问by ManOx
I've been using UIGraphicsBeginImageContextWithOptions()
and I noticed that the last field in that method is a scalefactor
. And the apple docs says, if I want to set it to 0.0, then it will use the default scale factor of the device's main screen. Is there a way to get that default scale factor via property or method?
我一直在使用UIGraphicsBeginImageContextWithOptions()
,我注意到该方法中的最后一个字段是scalefactor
. 苹果文档说,如果我想将它设置为 0.0,那么它将使用设备主屏幕的默认比例因子。有没有办法通过属性或方法获得默认比例因子?
回答by jrturton
The device's scale factor is a property of UIScreen
. You can obtain it by:
设备的比例因子是 的属性UIScreen
。您可以通过以下方式获取:
[UIScreen mainScreen].scale;
回答by Paulo Venturi
Swift 3+ version:
斯威夫特 3+ 版本:
UIScreen.main.scale
回答by Sudha Tiwari
Hope it will help you
希望它会帮助你
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;