How to get the devices scale factor?

2020-06-30 04:24发布

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?

3条回答
Luminary・发光体
2楼-- · 2020-06-30 04:34

The device's scale factor is a property of UIScreen. You can obtain it by:

[UIScreen mainScreen].scale;

Documentation here.

查看更多
一夜七次
3楼-- · 2020-06-30 04:45

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;
查看更多
Deceive 欺骗
4楼-- · 2020-06-30 04:48

Swift 3+ version:

UIScreen.main.scale
查看更多
登录 后发表回答