0
问题
我有一个CGImageRef
图像。它实际上是一个将用作蒙版的图像。所以,它的“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);
}
}
链接到项目
https://github.com/colasjojo/Test-NYSegmentedControl
显示在背景中以黑色和前景中的白色创建单词“Light”的代码,并显示如何创建和定位这些视图。 – matt
您正在使用_word_掩码,并且您正在使用变量_name_掩码,但在您的github项目中没有实际的掩码。所以你真的不应该声称这里有一个面具。没有。 – matt
@matt CGContextClipToMask(context,rect,mask);' – Colas