【问题标题】:Fill the remainder of a CGContext, keeping one rectangle transparent填充 CGContext 的其余部分,保持一个矩形透明
【发布时间】:2012-12-20 02:55:16
【问题描述】:

我想在绘制矩形后填充 CGCntext 的剩余部分?我怎样才能做到这一点?谢谢!


但问题是,我设置了 cgcontext kCGBlendModeClear 的混合模式。我想让上下文中的小矩形透明。如果先绘制背景,我还能在矩形中看到图像吗?

【问题讨论】:

  • 我对CGContext了解不多,但是可以设置背景,还是先用一个矩形画整个空间再画小一个?
  • 是的,你可以很容易地做到这一点。

标签: ios cgcontext


【解决方案1】:

如果要填充上下文(其框架为bigRect),但其中的一个矩形除外(其框架为smallRect):

CGContextBeginPath(context);
CGContextAddRect(context, bigRect);
CGContextAddRect(context, smallRect);
CGContextSetFillColorWithColor(context, color);
CGContextEOFillPath(context, bigRect);

【讨论】:

  • CGContextEOFillPath 只接受一个参数:CGContextEOFillPath(context);
【解决方案2】:

先做背景...

    CGRect bounds = [self bounds];
    [[UIColor blackColor] set];
    UIBezierPath* backgroundPath = [UIBezierPath bezierPathWithRect:bounds];
    [backgroundPath fill];

    CGRect innerRect = CGRectMake(self.bounds-10, 
                                  self.bounds-10, 
                                  self.bounds.width-20
                                  self.bounds.height-20);
    [[UIColor redColor] set];
    UIBezierPath* foregroundPath = [UIBezierPath bezierPathWithRect:innerRect];
    [foregroundPath fill];

【讨论】:

  • 但问题是,我设置了 cgcontext kCGBlendModeClear 的混合模式。我想让上下文中的小矩形透明。如果先绘制背景,我还能在矩形中看到图像吗?
  • 我想让剩下的部分透明。
  • 如果我不知道矩形是用红色填充的。在上下文中绘制图像。我还能得到 iamge 的小矩形或小圆圈吗?
  • 也许你应该发布一些代码和/或你想要达到的效果的图片
【解决方案3】:

绘制背景,然后使用CGContextClearRect 清除您想要透明的区域。

任意形状:

// do your foreground drawing

CGPathRef arbitraryShape; // asign your arbitrary shape
CGContextBeginPath(ctx);
CGContextAddRect(ctx, bounds); // rect in full size of the context
CGContextAddPath(ctx, arbitraryShape); // set the area you dont want to be drawn on
CGContextClip(ctx);

// do your background drawing

【讨论】:

  • 如果不是矩形?它可以是任意形状。路径有效吗?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-12-25
  • 1970-01-01
  • 1970-01-01
  • 2016-06-13
相关资源
最近更新 更多