首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >CGContextClipToMask不能裁剪

CGContextClipToMask不能裁剪
EN

Stack Overflow用户
提问于 2013-07-18 19:16:51
回答 2查看 927关注 0票数 2

此代码的结果如下图所示。就我对CGContextClipToMask的理解,红色矩形应该是不可见的,因为它在裁剪区域之外。这里我漏掉了什么?谢谢你的帮助!

代码语言:javascript
复制
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextSetFillColorWithColor(context, [UIColor blackColor].CGColor);
CGContextFillRect(context, rect);
CGContextSetLineWidth(context, 20);
CGContextSetStrokeColorWithColor(context, [UIColor whiteColor].CGColor);

// draw partial circle
UIBezierPath *arc   = [UIBezierPath bezierPathWithArcCenter:center radius:radius startAngle:startAngle endAngle:endAngle clockwise:NO];
CGContextAddPath(context, [arc CGPath]);
CGContextStrokePath(context);

// create mask
CGImageRef mask = CGBitmapContextCreateImage(context);
self.maskCreated(mask);

// save state
CGContextSaveGState(context);

// clip with mask
CGContextClipToMask(context, rect, mask);

// draw test rect
CGContextSetFillColorWithColor(context, [UIColor redColor].CGColor);
CGContextFillRect(context, CGRectMake(0, 0, 100, 100));

// restore state
CGContextRestoreGState(context);

EN

回答 2

Stack Overflow用户

发布于 2016-01-24 06:50:32

CGContextClipToMaskdocumentation说:

如果遮罩是图像,则它必须在DeviceGray颜色空间中,不能具有alpha分量,并且不能被图像遮罩或遮罩颜色遮罩。

我假设您的代码在UIView的子类的-drawRect:方法中,所以您使用的是提供给您的CGContext,它在RGB颜色空间中,可能有一个alpha分量。您的mask映像是从该上下文创建的,因此它具有相同的属性。

要解决此问题,请使用单独的位图上下文生成蒙版,并使用不带alpha的灰色色彩空间。下面是一个自包含的示例,它的功能与您的代码类似。

代码语言:javascript
复制
- (void)drawRect:(CGRect)rect
{
    // Create a context for the mask
    CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceGray();
    CGContextRef maskContext = CGBitmapContextCreate(NULL, rect.size.width, rect.size.height, 8, 0, colorSpace, kCGImageAlphaNone | kCGBitmapByteOrderDefault);
    CGColorSpaceRelease(colorSpace);

    // Fill with black
    CGContextSetFillColorWithColor(maskContext, [UIColor blackColor].CGColor);
    CGContextFillRect(maskContext, rect);

    // Draw an arc in white
    CGContextSetLineWidth(maskContext, 20);
    CGContextSetStrokeColorWithColor(maskContext, [UIColor whiteColor].CGColor);
    CGContextAddArc(maskContext, CGRectGetMidX(rect), CGRectGetMidY(rect), 50, M_PI, 0, false);
    CGContextStrokePath(maskContext);

    // Create the mask image from the context, and discard the context
    CGImageRef mask = CGBitmapContextCreateImage(maskContext);
    CGContextRelease(maskContext);

    // Now draw into the view itself
    CGContextRef context = UIGraphicsGetCurrentContext();

    // Apply the mask
    CGContextClipToMask(context, rect, mask);

    // Then draw something that overlaps the mask
    CGContextSetFillColorWithColor(context, [UIColor redColor].CGColor);
    CGContextFillRect(context, rect);

    // Make sure to clean up when we're done
    CGImageRelease(mask);
}
票数 1
EN

Stack Overflow用户

发布于 2013-07-18 20:38:43

实际上我不理解你的担心,但是你可以像这样在你的方法中隐藏矩形:

代码语言:javascript
复制
// draw a test rect             
CGContextSetFillColorWithColor(context, [UIColor redColor].CGColor);

CGRect rect = CGRectZero;
CGContextFillRect(context, rect);
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/17721959

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档