How to get the devices scale factor?

2020-06-30 04:35发布

问题:

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?

回答1:

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

[UIScreen mainScreen].scale;

Documentation here.



回答2:

Swift 3+ version:

UIScreen.main.scale


回答3:

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;