• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

ios - CGContextClipToMask 不剪辑

[复制链接]
菜鸟教程小白 发表于 2022-12-12 12:34:50 | 显示全部楼层 |阅读模式 打印 上一主题 下一主题

此代码生成下图。据我了解 CGContextClipToMask,红色矩形不应该是可见的,因为它在剪切区域之外。我在这里想念什么?感谢您的帮助!

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

Code Result



Best Answer-推荐答案


documentation对于 CGContextClipToMask 说:

If mask is an image, then it must be in the DeviceGray color space, may not have an alpha component, and may not be masked by an image mask or masking color.

我假设您的代码位于 UIView 子类的 -drawRect: 方法中,因此您使用的是 CGContext这是提供给您的,它位于 RGB 颜色空间中,并且可能具有 alpha 分量。您的 mask 图像是根据该上下文创建的,因此它具有相同的属性。

要解决此问题,请使用单独的位图上下文生成蒙版,并使用没有 alpha 的灰色色彩空间。这是一个与您的代码类似的独立示例。

- (void)drawRectCGRect)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);
}

关于ios - CGContextClipToMask 不剪辑,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17721959/

回复

使用道具 举报

懒得打字嘛,点击右侧快捷回复 【右侧内容,后台自定义】
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

关注0

粉丝2

帖子830918

发布主题
阅读排行 更多
广告位

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap