2015-09-27 51 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来获得你要去的效果。