我画了一个形状,如下所示:
- (void)drawRect:(CGRect)rect
{
// Draw a cross rectagle
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextSaveGState(context);
CGContextMoveToPoint(context, 190, 0);
CGContextAddLineToPoint(context, 220, 0);
CGContextAddLineToPoint(context, 310, 90);
CGContextAddLineToPoint(context, 310, 120);
CGContextSetFillColorWithColor(context, [UIColor lightGrayColor].CGColor);
CGContextFillPath(context);
CGContextRestoreGState(context);
}我在下面看到了一面深浅不一的十字旗

现在我想在我刚刚画的十字旗周围画一笔。
我应该怎么做才能做到这一点。请在这个问题上给我一些建议。谢谢。
发布于 2012-11-07 01:51:16
当然CGContextDrawPath(context, kCGPathFillStroke);就是你要找的
您可以使用以下命令调整图案和颜色:
CGContextSetStrokePattern
CGContextSetStrokeColor因此,在你的例子中,假设你想要一个纯黑色的笔触,你应该有:
- (void)drawRect:(CGRect)rect
{
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextSaveGState(context);
CGContextSetLineWidth(context, 2.0);
CGContextSetStrokeColorWithColor(context, [UIColor blackColor].CGColor);
CGContextSetFillColorWithColor(context, [UIColor lightGrayColor].CGColor);
CGContextMoveToPoint(context, 190, 0);
CGContextAddLineToPoint(context, 220, 0);
CGContextAddLineToPoint(context, 310, 90);
CGContextAddLineToPoint(context, 310, 120);
CGContextClosePath(context);
CGContextDrawPath(context, kCGPathFillStroke);
CGContextFillPath(context);
CGContextRestoreGState(context);
}产生:

发布于 2012-11-07 03:43:18
- (void)drawRect:(CGRect)rect
{
// Draw a cross rectagle
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextSaveGState(context);
//New
CGContextSetLineWidth(context, 2.0);
CGContextMoveToPoint(context, 190, 0);
CGContextAddLineToPoint(context, 220, 0);
CGContextAddLineToPoint(context, 310, 90);
CGContextAddLineToPoint(context, 310, 120);
//New
CGContextSetStrokeColorWithColor(context, [UIColor blackColor].CGColor);
CGContextSetFillColorWithColor(context, [UIColor lightGrayColor].CGColor);
CGContextFillPath(context);
//New
CGContextStrokePath(context);
CGContextRestoreGState(context);
}@WDUK :花了几个小时才弄清楚,我知道你上面的答案为什么不起作用了。原因是当您第一次执行CGContextFillPath时,路径最终会被清除,然后您不能再在其上执行CGContextStrokePath。因此,为了做CGContextFillPath和CGContextStrokePath,我们必须做
上下文CGContextDrawPath(context,kCGPathFillStroke);
在尝试之后,我得到了以下结果

https://stackoverflow.com/questions/13256436
复制相似问题