iOS:如何使用 CGLayer 支持 Retina 显示屏?
我正在 CALayer 的委托方法 drawLayer:inContext:
中绘制图形。
现在我想支持 Retina Display,因为图形在最新设备上看起来很模糊。
对于直接在 CALayer 传递的图形上下文上绘制的部分,我可以通过如下设置 CALayer 的 contentScale 属性来很好地以高分辨率绘制。
if ([myLayer respondsToSelector:@selector(setContentsScale:)]) {
myLayer.contentsScale = [[UIScreen mainScreen] scale];
}
但是对于我使用CGLayer的部分仍然绘制得模糊。
如何在 CGLayer 上以高分辨率绘图以支持 Retina 显示?
我想使用 CGLayer 重复绘制图形的相同绘图形状,以及剪掉超出图层边缘的图形线。
我通过 CGLayerCreateWithContex
获取 CGLayer,并使用从 CALayer 传递的图形上下文,并使用 CG 函数(例如 CGContextFillPath
或 CGContextAddLineToPoint
)在其上下文上进行绘制。
我需要支持 iOS 4.x 和 iOS 3.1.3、Retina 和传统显示屏。
谢谢,
库拉
I'm drawing a graph on a CALayer in its delegate method drawLayer:inContext:
.
Now I want to support Retina Display, as the graph looks blurry on the latest devices.
For the parts that I draw directly on the graphics context passed by the CALayer, I could nicely draw in high resolution by setting the CALayer's contentScale property as follows.
if ([myLayer respondsToSelector:@selector(setContentsScale:)]) {
myLayer.contentsScale = [[UIScreen mainScreen] scale];
}
But for the parts that I use CGLayer are still drawn blurry.
How do I draw on a CGLayer in high resolution to support Retina Display?
I want to use CGLayer to draw the same plot shapes of the graph repeatedly, as well as to cut off the graph lines exceeding the edge of the layer.
I get CGLayer by CGLayerCreateWithContex
with the graphics context passed from the CALayer, and draw on its context using CG functions such as CGContextFillPath
or CGContextAddLineToPoint
.
I need to support both iOS 4.x and iOS 3.1.3, both Retina and legacy display.
Thanks,
Kura
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这是为所有分辨率正确绘制 CGLayer 的方法。
首次创建图层时,您需要通过将尺寸乘以比例来计算正确的边界:
然后您需要为图层上下文设置正确的比例:
如果当前设备具有视网膜显示屏,则对图层进行的所有绘制现在都将绘制两倍大。
>
当您最终绘制图层的内容时,请确保使用 CGContextDrawLayerInRect 并提供未缩放的 CGRect:
就是这样!
This is how to draw a CGLayer correctly for all resolutions.
When first creating the layer, you need to calculate the correct bounds by multiplying the dimensions with the scale:
You then need to set the correct scale for your layer context:
If the current device has a retina display, all drawing made to the layer will now be drawn twice as large.
When you finally draw the contents of your layer, make sure you use CGContextDrawLayerInRect and supply the unscaled CGRect:
That's it!
我决定不使用 CGLayer 并直接在 CALayer 的图形上下文上绘制,现在它在视网膜显示屏上以高分辨率绘制得很好。
我发现了类似的问题这里< /a>,发现在我的例子中使用 CGLayer 没有意义。
我使用CGLayer是因为Apple的示例程序"使用多个CGLayer绘制旗帜的对象” 可在 Quartz 2D 编程指南中找到。在此示例中,它为一颗星星创建一个 CGLayer 并多次使用它来绘制 50 颗星星。我认为这是出于性能原因,但我没有看到任何性能差异。
为了切断超出图层边缘的图形线,我决定使用多个CALayers。
I decided not to use CGLayer and directly draw on the graphics context of the CALayer, and now it's drawn nicely in high resolution on retina display.
I found the similar question here, and found that there is no point of using CGLayer in my case.
I used CGLayer because of the Apple's sample program "Using Multiple CGLayer Objects to Draw a Flag" found in the Quartz 2D Programming guide. In this example, it creates one CGLayer for a star and uses it multiple times to draw 50 stars. I thought this was for a performance reason, but I didn't see any performance difference.
For the purpose of cutting off the graph lines exceeding the edge of the layer, I decided to use multiple CALayers.