2015-09-27 89 views
0

問題

我有一個CGImageRef圖像。它實際上是一個將用作蒙版的圖像。所以,它的「alpha層」就是我真正關心的。我想「加厚」圖像的不透明部分,就好像我會在不透明部分周圍添加筆觸。怎麼做?如何在圖像上添加筆劃?

更多的上下文

我有這樣的圖像(CGImageRef):

enter image description here

已通過

mask = CGBitmapContextCreateImage(context); 

創建我用它作爲掩模。掩碼下的UIView是相同的文本。問題是下面的文本沒有被掩碼覆蓋。請參閱:

enter image description here

更多代碼

此代碼是在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

+1

顯示在背景中以黑色和前景中的白色創建單詞「Light」的代碼,並顯示如何創建和定位這些視圖。 – matt

+0

您正在使用_word_掩碼,並且您正在使用變量_name_掩碼,但在您的github項目中沒有實際的掩碼。所以你真的不應該聲稱這裏有一個面具。沒有。 – matt

+0

@matt CGContextClipToMask(context,rect,mask);' – Colas

回答

1

如果你想夾到文本,我建議不要通過中間位這樣做,而是剪輯的實際文本的路徑在從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來獲得你要去的效果。