【问题标题】:core graphics CGContextClip - Xcode核心图形 CGContextClip - Xcode
【发布时间】:2012-07-09 15:02:06
【问题描述】:

开始了解核心图形。我正在绘制一条带有填充和笔划的路径,但我无法对其进行剪辑。谁能告诉我我在这里做错了什么?

这段代码在我的 UIView 子类的 drawRect 方法中。

//Draw a closed path with rounded corners, a fill and a stroke.
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextSetLineWidth(context, 4.0);
CGContextSetStrokeColorWithColor(context,[UIColor blueColor].CGColor);
CGContextSetFillColorWithColor(context, [UIColor redColor].CGColor);
//fill
CGContextMoveToPoint(context, 10, 10);
CGContextAddLineToPoint(context, 10, 100);
CGContextAddArcToPoint(context, 10,140, 30,140, 20);
CGContextAddLineToPoint(context, 200, 140);
CGContextAddArcToPoint(context, 240,140, 240,100, 20);
CGContextAddLineToPoint(context, 240, 10);
CGContextClosePath(context);
CGContextFillPath(context);
//stroke
CGContextMoveToPoint(context, 10, 10);
CGContextAddLineToPoint(context, 10, 100);
CGContextAddArcToPoint(context, 10,140, 30,140, 20);
CGContextAddLineToPoint(context, 200, 140);
CGContextAddArcToPoint(context, 240,140, 240,100, 20);
CGContextAddLineToPoint(context, 240, 10);
CGContextClosePath(context);
CGContextStrokePath(context);
CGContextBeginPath(context);
//clip??
CGContextMoveToPoint(context, 10, 10);
CGContextAddLineToPoint(context, 10, 100);
CGContextAddArcToPoint(context, 10,140, 30,140, 20);
CGContextAddLineToPoint(context, 200, 140);
CGContextAddArcToPoint(context, 240,140, 240,100, 20);
CGContextAddLineToPoint(context, 240, 10);
CGContextClosePath(context);
CGContextClip(context);

【问题讨论】:

    标签: xcode core-graphics clipping


    【解决方案1】:

    你应该在做应该剪裁的图纸之前先剪裁上下文,然后在之后恢复上下文

    【讨论】:

      【解决方案2】:

      您应该在CGContextStrokePath 之前和CGContextClosePath 之后使用CGContextClip。够了。无需编写额外的代码。像下面这样尝试..

          CGContextClosePath(context);
          CGContextClip(context);
          CGContextStrokePath(context);
      

      我认为这对你会有帮助。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多