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