我正在 CALayer 的委托方法中绘制图表drawLayer:inContext:
.
现在我想支持 Retina Display,因为图形在最新设备上看起来很模糊。
对于直接在 CALayer 传递的图形上下文上绘制的部分,我可以通过如下设置 CALayer 的 contentScale 属性来很好地以高分辨率绘制。
if ([myLayer respondsToSelector:@selector(setContentsScale:)]) {
myLayer.contentsScale = [[UIScreen mainScreen] scale];
}
但是对于我使用CGLayer的部分仍然绘制得模糊。
如何在 CGLayer 上以高分辨率绘图以支持 Retina 显示?
我想使用 CGLayer 重复绘制图形的相同绘图形状,以及剪掉超出图层边缘的图形线。
我通过以下方式获取CGLayerCGLayerCreateWithContex
使用从 CALayer 传递的图形上下文,并使用 CG 函数在其上下文上进行绘制,例如CGContextFillPath
or CGContextAddLineToPoint
.
我需要支持 iOS 4.x 和 iOS 3.1.3、Retina 和传统显示屏。
Thanks,
Kura
这是为所有分辨率正确绘制 CGLayer 的方法。
-
首次创建图层时,您需要通过将尺寸乘以比例来计算正确的边界:
int width = 25;
int height = 25;
float scale = [self contentScaleFactor];
CGRect bounds = CGRectMake(0, 0, width * scale, height * scale);
CGLayer layer = CGLayerCreateWithContext(context, bounds.size, NULL);
CGContextRef layerContext = CGLayerGetContext(layer);
-
然后,您需要为图层上下文设置正确的比例:
CGContextScaleCTM(layerContext, scale, scale);
如果当前设备具有视网膜显示屏,则对图层进行的所有绘制现在都将绘制两倍大。
-
当您最终绘制图层的内容时,请确保使用 CGContextDrawLayerInRect 并提供未缩放的 CGRect:
CGRect bounds = CGRectMake(0, 0, width, height);
CGContextDrawLayerInRect(context, bounds, layerContext);
就是这样!
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)