作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我有一张 CGImageRef
图片。它实际上是一个将用作 mask 的图像。所以,它的“alpha 层”才是我真正关心的。我想“加厚”图像的不透明部分,就好像我要在不透明部分周围添加笔触一样。如何做到这一点?
我有一个像这样的图像(CGImageRef
):
它是由
创建的 mask = CGBitmapContextCreateImage(context);
我把它当作面具使用。掩码下的 UIView
是相同的文本。问题是下面的文本没有被掩码覆盖。见:
此代码在 UILabel
子类中。
- (void)drawRect:(CGRect)rect {
CGContextRef context = UIGraphicsGetCurrentContext();
// Draw text normally
[super drawTextInRect:rect];
if (self.alternativeTextColor) {
CGImageRef mask = NULL;
// Create a mask from the text
mask = CGBitmapContextCreateImage(context);
CGContextSaveGState(context);
CGContextTranslateCTM(context, 0, self.frame.size.height);
CGContextScaleCTM(context, 1.0, (CGFloat) -1.0);
// Clip the current context to our mask
CGContextClipToMask(context, rect, mask);
// Set fill color
CGContextSetFillColorWithColor(context, [self.alternativeTextColor CGColor]);
// Path from mask
CGPathRef path;
if (CGRectIsEmpty(self.maskFrame)) {
path = CGPathCreateMutable();
} else {
UIBezierPath *roundRectBezierPath = [UIBezierPath bezierPathWithRoundedRect:self.maskFrame
cornerRadius:self.maskCornerRadius];
path = CGPathCreateCopy([roundRectBezierPath CGPath]);
}
CGContextAddPath(context, path);
// Fill the path
CGContextFillPath(context);
CFRelease(path);
// Clean up
CGContextRestoreGState(context);
CGImageRelease(mask);
}
}
最佳答案
如果您尝试剪切到文本,我建议不要通过中间位图来执行此操作,而是剪切到实际文本的路径,如下面来自 SVGgh 的 GHText 类的片段:
CGContextSaveGState(quartzContext);
CGContextSetTextDrawingMode(quartzContext, kCGTextStrokeClip);
CGContextSetLineWidth(quartzContext, strokeWidthToUse);
... Draw Text here ....
CGContextReplacePathWithStrokedPath(quartzContext);
CGContextClip(quartzContext);
... Do clipped drawing here ...
CGContextSetTextDrawingMode(quartzContext, kCGTextFill);
CGContextRestoreGState(quartzContext);
这并没有回答您的问题,但这可能是您应该做的。尝试使用 CGContextSetTextDrawingMode 来获得您想要的效果。
关于ios - 如何在图像周围添加笔触?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32813941/
我想实现以下目标: 在 Canvas 上绘制背景图像(一次或如果需要重复) 图片不应该在开头可见 当我在 Canvas 上“绘制”形状时,bg-image 应该在绘制形状的位置可见 将显示的图像部分应
我是一名优秀的程序员,十分优秀!