核心图形绘制线与轮廓 [英] Core Graphics draw line with outline

查看:92
本文介绍了核心图形绘制线与轮廓的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用Core Graphics绘制一条宽度为4像素的任意线,现在我想这条线有另一种颜色的1像素轮廓。我看不到任何可以实现这个开箱即用的CG功能,但我正在寻找如何做到的建议。这是我现有的代码:

I'm drawing an arbitrary line with Core Graphics with a width of 4 pixels, now I would like this line to have a 1 pixel outline of another colour. I can't see any CG functions that would achieve this "out of the box" but I'm looking for suggestions on how it could be done. This is my existing code:

CGContextRef context = UIGraphicsGetCurrentContext();
CGContextSetLineWidth(context, 4.0);

CGPoint curPoint = [(NSValue*)[points objectAtIndex:0] CGPointValue];
CGContextMoveToPoint(context, curPoint.x, curPoint.y);

for( int i = 1; i < [points count]; i++ ) {
    curPoint = [(NSValue*)[points objectAtIndex:i] CGPointValue];
    CGContextAddLineToPoint(context, curPoint.x, curPoint.y);
    CGContextStrokePath(context);
    CGContextMoveToPoint(context, curPoint.x, curPoint.y);
}

这将生成单行。我想生成一个4px的线1px行突出显示4px行像这样:

This produces the single line. I would like to produce a 4px line with a 1px line highlighting the 4px line like this:

推荐答案

iOS 5.0添加了一个新功能 CGPathCreateCopyByStrokingPath / code>这是你想要的。

iOS 5.0 added a new feature CGPathCreateCopyByStrokingPath() that does what you want.

首先为黑色路径创建一个 CGPathRef 然后使用 CGPathCreateCopyByStrokingPath()创建一个副本。

First create a CGPathRef for the black path, and then create a copy of it with CGPathCreateCopyByStrokingPath().

这将给你一个新的路径,

This will give you a new path, which you can fill in black and stroke in red, to get what you want.

此外,创建路径有点慢。你应该避免在执行屏幕绘制时创建路径。所有路径都应存储在RAM中,并且可以在之前开始绘制到屏幕。 drawRect:应该只绘制路径,而不是创建它。

Also, creating paths is a bit slow. You should avoid creating paths while performing screen drawing. All your paths should be stored in RAM and ready to go before you start drawing to the screen. drawRect: should only draw the path, not create it.

这篇关于核心图形绘制线与轮廓的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆