本文介绍了绘制一个多边形的多边形用于描边,另一个用于填充?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在绘制一些用颜色描边的线条然后用另一条线填充它们的内部(它们形成一个多边形)时遇到了麻烦。

I'm having trouble with drawing some lines that are stroked with a color and then filling their insides (they make a polygon) with another one.

UIColor *houseBorderColor = [UIColor colorWithRed:(170/255.0) green:(138/255.0) blue:(99/255.0) alpha:1];
CGContextSetStrokeColorWithColor(context, houseBorderColor.CGColor);
CGContextSetLineWidth(context, 3);

// Draw the polygon
CGContextMoveToPoint(context, 20, viewHeight-19.5);
CGContextAddLineToPoint(context, 200, viewHeight-19.5); // base
CGContextAddLineToPoint(context, 300, viewHeight-119.5); // right border
CGContextAddLineToPoint(context, 120, viewHeight-119.5);
CGContextAddLineToPoint(context, 20, viewHeight-19.5);

// Fill it
CGContextSetRGBFillColor(context, (248/255.0), (222/255.0), (173/255.0), 1);
//CGContextFillPath(context);

// Stroke it
CGContextStrokePath(context);

CGContextStrokePath 注释掉时,我得到了结果:

With the CGContextStrokePath commented out, I get this result:

但如果我取消注释 CGContextStrokePath 并填写多边形,颜色会溢出笔画:

But if I uncomment CGContextStrokePath and fill out the polygon, the color overflows the strokes:

如何实现这样的结果(无需重做整个绘图程序两次):

How do you achieve a result like this (without having to redo the whole drawing procedure twice):

推荐答案

您可以使用

CGContextDrawPath(context, kCGPathFillStroke);

而不是

CGContextFillPath(context);
CGContextStrokePath(context);

问题是 CGContextFillPath() CGContextStrokePath(上下文)
清除当前路径,以便只有第一个操作成功,第二个
操作什么都没画。 CGContextDrawPath()结合填充和描边而不用
清除其间的路径。

The problem is that both CGContextFillPath() and CGContextStrokePath(context)clear the current path, so that only the first operation succeeds, and the secondoperation draws nothing. CGContextDrawPath() combines fill and stroke withoutclearing the path in between.

这篇关于绘制一个多边形的多边形用于描边,另一个用于填充?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-22 16:21