- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我试图通过使用剪贴蒙版剪掉路径的笔划,只留下阴影,在圆角矩形内绘制一个嵌入阴影。最终目标是近似通知中心内事物的嵌入外观。
我失败了。 CGContextClipToMask 根本不起作用,这尤其令人抓狂,因为我在项目的其他地方使用了同样的技术,而且在那里工作正常。这是我的绘图代码:
const CGFloat cornerRadius = 5.0f;
CGContextRef context = UIGraphicsGetCurrentContext();
// Draw background color
[[UIColor colorWithWhite:0.2 alpha:0.5] setFill];
UIBezierPath* background = [UIBezierPath bezierPathWithRoundedRect:self.bounds cornerRadius:cornerRadius];
[background fill];
// Draw the inner shadow
UIImage* mask;
UIGraphicsBeginImageContext(self.bounds.size);
{{
CGContextRef igc = UIGraphicsGetCurrentContext();
[[UIColor blackColor] setFill];
CGContextFillRect(igc, self.bounds);
// Inset the mask by a ridiculous degree just to be sure that it's working
UIBezierPath* insetMask = [UIBezierPath bezierPathWithRoundedRect:CGRectInset(self.bounds, 20, 20) cornerRadius:cornerRadius];
[[UIColor whiteColor] setFill];
[insetMask fill];
mask = UIGraphicsGetImageFromCurrentImageContext();
}}
UIGraphicsEndImageContext();
CGContextSaveGState(context);
{{
CGContextClipToMask(context, self.bounds, mask.CGImage);
[[UIColor whiteColor] setStroke];
CGContextSetShadow(context, CGSizeMake(0, 1), 1);
[background stroke];
}}
CGContextRestoreGState(context);
如果我要猜测出了什么问题,我会说我以某种方式违反了文档中规定的先决条件:
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.
但是,我发现没有办法验证是否是这种情况,更不用说纠正了。
TIA,感谢您提供的任何帮助!
(我在运行 iOS 6 的 iPhone 4S 上运行该应用程序。)
最佳答案
您不能使用 UIGraphicsBeginImageContext
创建单 channel 图像。您必须使用 CGBitmapContextCreate
。您也没有通过使用 UIGraphicsBeginImageContext
而不是 UIGraphicsBeginImageContextWithOptions
正确处理 Retina。
让我向您展示一种绘制内部阴影的可能更简单的方法,它不需要弄乱位图上下文和图像。当您有一条非自相交路径时,您可以通过用一个大矩形包围它并使用奇偶填充规则来反转它。
这是一个例子。我制作了一个 InnerShadowView
。这是它的 drawRect:
:
- (void)drawRect:(CGRect)rect {
[self fillIfNeeded];
[self drawShadowIfNeeded];
}
fillIfNeeded
方法只是绘制实心圆角矩形:
- (void)fillIfNeeded {
UIColor *color = self.fillColor;
if (!color)
return;
[color setFill];
UIBezierPath *path = [UIBezierPath bezierPathWithRoundedRect:self.bounds
cornerRadius:self.cornerRadius];
[path fill];
}
drawShadowIfNeeded
方法绘制内阴影。首先,我剪辑到圆角矩形:
- (void)drawShadowIfNeeded {
UIColor *color = self.shadowColor;
if (!color)
return;
CGContextRef gc = UIGraphicsGetCurrentContext();
CGContextSaveGState(gc); {
[[UIBezierPath bezierPathWithRoundedRect:self.bounds
cornerRadius:self.cornerRadius] addClip];
接下来,我构建了一个圆角矩形的反面:一个包含所有圆角矩形的路径。我从一个非常大的矩形开始:
UIBezierPath *invertedPath = [UIBezierPath bezierPathWithRect:CGRectInfinite];
然后我将圆角矩形附加到同一路径:
[invertedPath appendPath:[UIBezierPath bezierPathWithRoundedRect:CGRectInset(self.bounds, -1, -1) cornerRadius:self.cornerRadius]];
请注意,我稍微扩大了圆角矩形。当拐角半径不为零时,这是必要的,以避免曲线周围出现一些伪影。
接下来,我将复合路径设置为使用偶数/奇数填充规则:
invertedPath.usesEvenOddFillRule = YES;
通过使用偶数/奇数填充规则,我确保无限矩形和圆角矩形内的任何点都被排除在填充区域之外,而圆角矩形之外的任何点都是包括。
现在,当我填充这个倒置路径时,它会尝试填充圆角矩形之外的每个像素。它唯一可以转换阴影的地方是圆角矩形内。由于我已经剪裁到圆角矩形,它只会绘制阴影。
所以我设置了填充参数并填充了路径:
CGContextSetShadowWithColor(UIGraphicsGetCurrentContext(),
self.shadowOffset, self.shadowBlur, color.CGColor);
[[UIColor blackColor] setFill];
[invertedPath fill];
并清理:
} CGContextRestoreGState(gc);
}
用红色阴影颜色,看起来像这样:
你可以找到所有的代码in this gist便于下载。
关于iphone - CGContextClipToMask 不剪裁,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13595284/
在this question的回答中, this fiddle进行“正常”裁剪,即灰色区域矩形外的图像将被裁剪,只保留灰色矩形区域内的图像。 “正常”裁剪的关键代码使用函数“clipTo”: obje
我一直在使用 JPanel,在提供的代码中名为 infoPanel,我希望能够在它前面精确地放置一个带有 JLabel 的 JTextField。我尝试向其中添加 JLabel,但无法将其放置在我想要
这可能没有我想象的那么难,但是我如何选择图像的一部分呢?考虑以下示例: 灰色区域是PNG或JPG格式的图片,现在我想从中选择红色的80x80像素区域。红色区域应显示,其余区域不显示。我尝试了很多方法:
http://www.youtube.com/watch?v=gZNdfVwkttM - 如果您看不到图片,您可以看到此视频中描述的所有问题。 下面所有图像中的所有墙壁都具有半透明的 PNG 纹理。每
我已经阅读了几个小时关于剪辑的文章,但我似乎没有找到解决我的问题的方法。 这是我的场景: 在 OpenGL ES 环境(IOS、Android)中,我有一个由可绘制对象组成的 2D 场景图,形成一棵树
问题是当我使用像 Helvetica Oblique 这样大小大于 13 的斜体字体时,UIButton 剪辑标题尾部(最后大约 1-2 个像素)。有人知道这个问题的解决方案吗? 最佳答案 子类 UI
想象一下,有一组光线围绕着一个圆圈 - 就像太阳一样。我想做的是遮盖光线,让光线看起来像是从太阳“升起”。 在 Flash 中,我可以通过执行类似以下步骤的操作为此创建一个不错的效果:- 将光线分成
我试图在背景图片上添加 CSS3 webkit 模糊,然后对其进行剪辑(通过将其放入较小的父级并设置 overflow: hidden ) 不幸的是,当我这样做时,我的模糊效果只发生在剪辑的内容上,所
如何让 div #1(页面标题)“切入”div #2(mainwrap)并查看背景图像? 例子: CSS: body {background-image: url("/background
我有一个相当数学的问题需要解决: 任务是用最少的废料从固定长度的管中切割出预定数量的管。 假设我想从标准长度为 6m 的 pipe 中切割出 10 根 1m 的 pipe 和 20 根 2.5m 的
我有一个巨大的image嵌入在 SVG 中的 map ,它比浏览器窗口大得多并位于屏幕中心。 有两条路。一条路径勾勒出一座建筑。另一条路径勾勒出 map 上的一个子区域。
我试图理解 E. L Foster 和 J. R. Overfelt [1] 的论文“Clipping of Arbitrary Polygons with Degeneracies”,该论文声称通过
我正在从一组点计算 voronoi 图,如下所示: from scipy.spatial import Voronoi import numpy as np np.random.seed(0) poi
我整天都在尝试解决这个问题,我将 mainWindow.xib 升级到了 4 英寸 iPhone 5 版本,并且我所有的 ActionSheets 在 4 英寸模拟器上都表现正常。但是,如果我取消在
我做了一个这样的面板框: HTML Hot Article Article Article Article
有什么方法可以防止 clip-path 剪掉它的 child 吗?例如,考虑以下代码: .el { width: 300px; height: 300px; clip-path: poly
我的应用程序使用 UINavigationController,但我没有显示工具栏,因为我的所有导航都由游戏内控件控制。 我没有标签栏,因为我没有使用 TabBarController。 我的游戏应用
场景: 在我的场景中,我实现了一个顶点着色器,它在相机位置的 xz 轴上定位一个平面网格。因此,如果相机移动,平面网格也会随之移动。这导致视觉效果,即在移动相机时,平面网格似乎保持固定在原地。这似乎工
我想替换列表中的大纲。因此我定义了一个上限和下限。现在,upper_bound 和 lower_bound 之下的每个值都替换为绑定(bind)值。我的方法是使用 numpy 数组分两步执行此操作。
我想在我的 Fabric-powered Canvas 中设置一个全局 clipTo,这将影响所有用户添加的图层。我想要一个不受此剪辑蒙版影响的背景图像和叠加图像。 例子: 这是这张照片中发生的事情:
我是一名优秀的程序员,十分优秀!