- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
此代码生成下图。据我了解 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);
最佳答案
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
图像是根据该上下文创建的,因此它具有相同的属性。
要解决此问题,请使用单独的位图上下文生成 mask ,使用没有 alpha 的灰色色彩空间。这是一个独立的示例,它执行与您的代码类似的操作。
- (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);
}
关于ios - CGContextClipToMask 不剪辑,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17721959/
到目前为止,我正在尝试在 iOS 上复制 CGContextClipToMask 的行为,但运气不佳。有谁知道 CGContextClipToMask 在内部是如何工作的?我已经阅读了文档,它说它只是
我试图通过使用剪贴蒙版剪掉路径的笔划,只留下阴影,在圆角矩形内绘制一个嵌入阴影。最终目标是近似通知中心内事物的嵌入外观。 我失败了。 CGContextClipToMask 根本不起作用,这尤其令人抓
此代码生成下图。据我了解 CGContextClipToMask,红色矩形不应该可见,因为它在裁剪区域之外。我在这里错过了什么?感谢您的帮助! CGContextRef context = UIGra
我是 Quartz 新手。我有 2 张图像、一个背景和一个带有切口形状的蒙版,我想将其放在背景上以切出一个部分。生成的图像应该是切口的形状。这是我的面具(中间的形状是 0 alpha): 这是我的代码
我正在尝试将 UIImage 保存到照片库中,并使用以下方法来执行此操作: func savePhoto() { UIGraphicsBeginImageContextWithOptions(
我正在尝试为我的图像添加剪辑蒙版。然后用手指在屏幕上触摸作画。 手指触摸和剪辑蒙版有效,但问题是我的 UIImage 看起来上下颠倒。 我该如何纠正? 这是我的面具的代码行: CGContextCli
是否可以让 CGContextClipToMask 忽略蒙版图像的灰度值并像纯黑白一样工作? 我有一个灰度图像,当我将它用作 mask 时,灰色被解释为 alpha channel 。这很好,除了我需
我无法在 Internet 上找到一个示例来教我如何即时创建一个圆圈,然后使用该圆圈来剪辑 UIImage。 这是我的代码,不幸的是它没有给我想要的结果。 //create a graphics co
我是一名优秀的程序员,十分优秀!