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