Ios 填充CGContext的其余部分,使一个矩形保持透明

Ios 填充CGContext的其余部分,使一个矩形保持透明,ios,cgcontext,Ios,Cgcontext,我想在绘制rect后填充CGCntext的剩余部分?我该怎么做?谢谢大家! 但问题是,我将cgcontext kCGBlendModeClear的混合模式设置为Clear。我想让上下文中的小矩形变得透明。如果先画背景,我还能看到矩形中的图像吗?先画背景 CGRect bounds = [self bounds]; [[UIColor blackColor] set]; UIBezierPath* backgroundPath = [UIBezierPath bezier

我想在绘制rect后填充CGCntext的剩余部分?我该怎么做?谢谢大家!



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

先画背景

    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];

绘制背景,然后使用
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

如果要填充上下文(其框架为
bigRect
),除了其内部的矩形(其框架为
smallRect
):


我对CGContext了解不多,但是你能设置背景吗,或者在绘制较小的矩形之前,你能用矩形绘制整个空间吗?是的,你可以很容易地做到。但问题是,我将CGContext的混合模式设置为kCGBlendModeClear。我想让上下文中的小矩形变得透明。如果a先画背景,我还能看到矩形中的图像吗?我想使再制作部分透明。如果我不知道矩形中填充了红色。在上下文上绘制图像。我还能得到小矩形或小圆圈吗?如果不是矩形,也许你应该发布一些代码和/或你想要达到的效果的图片?它可以是任意形状。路径有效吗?CgContextOfillPath只接受一个参数:CgContextOfillPath(上下文);
CGContextBeginPath(context);
CGContextAddRect(context, bigRect);
CGContextAddRect(context, smallRect);
CGContextSetFillColorWithColor(context, color);
CGContextEOFillPath(context, bigRect);