0

使用核心图形,我将如何绘制一个向中心增加 alpha 值的羽化圆?像这样的东西:

在此处输入图像描述

理想情况下,颜色、圆半径和 alpha 强度将是参数。

4

2 回答 2

2

这在我的一些应用程序中对我有用。柔软度为 0-1。您需要自己制作一个宽度和高度为宽度(正方形)的 CGBitmapContext。

// make a path:
self.shapePath = CGPathCreateMutable();
CGPathAddEllipseInRect(self.shapePath, NULL, CGRectMake(0, 0, width, width));
CGFloat radius = width / 2.0f;        
NSInteger innerRadius = (softness < 0.01f ? (NSInteger)radius : (NSInteger)ceil((1.0f - softness) * radius));
NSInteger outerRadius = (NSInteger)ceil(radius);
CGFloat alphaStep = MAX(0.0059f, (1.0f / ((outerRadius - innerRadius) + 1)));
CGFloat outerMultiplier = 1.0f / (2.0f * (CGFloat)outerRadius);

for (NSInteger i = outerRadius; i >= innerRadius; --i)
{
    CGContextSaveGState(bitmapContext);
    UIColor* c = [UIColor.whiteColor colorWithAlphaComponent:alphaStep];
    CGContextTranslateCTM(bitmapContext, outerRadius - i, outerRadius - i);
    CGFloat scale = (2.0f * (CGFloat)i) * outerMultiplier;
    CGContextScaleCTM(bitmapContext, scale, scale);
    CGContextSetFillColorWithColor(bitmapContext, c.CGColor);
    CGContextAddPath(bitmapContext, self.shapePath);
    CGContextEOFillPath(bitmapContext);
    CGContextRestoreGState(bitmapContext);
}
于 2013-11-12T22:33:18.763 回答
1

您可以使用 Core GraphicsCGGradientRefCGContextDrawRadialGradient() 函数,也可以使用 Core 图像CIRadialGradient过滤器。您使用哪一种取决于您的需求。

于 2013-11-12T22:02:18.060 回答