iOS:如何使用 CGLayer 支持 Retina 显示屏?

发布于 2024-11-24 20:47:34 字数 727 浏览 2 评论 0原文

我正在 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 函数(例如 CGContextFillPathCGContextAddLineToPoint)在其上下文上进行绘制。

我需要支持 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

愛放△進行李 2024-12-01 20:47:34

这是为所有分辨率正确绘制 CGLayer 的方法。

  1. 首次创建图层时,您需要通过将尺寸乘以比例来计算正确的边界:

    int 宽度 = 25; 
    整数高度=25;
    浮动比例= [自身内容比例因子];
    CGRect 边界 = CGRectMake(0, 0, 宽度 * 比例, 高度 * 比例);
    CGLayer 层 = CGLayerCreateWithContext(context,bounds.size,NULL);
    CGContextRef 层上下文 = CGLayerGetContext(层);
    
  2. 然后您需要为图层上下文设置正确的比例:

    CGContextScaleCTM(layerContext, 比例尺, 比例尺);
    
  3. 如果当前设备具有视网膜显示屏,则对图层进行的所有绘制现在都将绘制两倍大。

    >

  4. 当您最终绘制图层的内容时,请确保使用 CGContextDrawLayerInRect 并提供未缩放的 CGRect:

    CGRect 边界 = CGRectMake(0, 0, 宽度, 高度);
    CGContextDrawLayerInRect(上下文,边界,layerContext);
    

就是这样!

This is how to draw a CGLayer correctly for all resolutions.

  1. When first creating the layer, you need to calculate the correct bounds by multiplying the dimensions with the scale:

    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);
    
  2. You then need to set the correct scale for your layer context:

    CGContextScaleCTM(layerContext, scale, scale);
    
  3. If the current device has a retina display, all drawing made to the layer will now be drawn twice as large.

  4. When you finally draw the contents of your layer, make sure you use CGContextDrawLayerInRect and supply the unscaled CGRect:

    CGRect bounds = CGRectMake(0, 0, width, height);
    CGContextDrawLayerInRect(context, bounds, layerContext);
    

That's it!

欲拥i 2024-12-01 20:47:34

我决定不使用 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文