我在iPhone上使用CALayer绘制时遇到了奇怪的问题。我有一个根层,它添加了一堆表示“气泡”的子层。最终结果应该看起来像这样:http://www.expensivedna.com/IMG_0018.PNG http://www.expensivedna.com/IMG_0018.PNG。问题是,我似乎无法使图层反锯齿(请注意气泡上的锯齿)。我的代码覆盖了气泡CALayer的drawInContext方法,如下所示:
- (void)drawInContext:(CGContextRef)theContext{
CGContextSetAllowsAntialiasing(theContext, true);
CGContextSetShouldAntialias(theContext, true);
size_t num_locations = 2;
CGFloat locations[2] = { 0.0, 1.0 };
CGFloat components[8] = { 1.0, 1.0, 1.0, 0.5, // Start color
1.0, 1.0, 1.0, 0.0 }; // End color
CGColorSpaceRef rgbColorspace = CGColorSpaceCreateDeviceRGB();
CGGradientRef glossGradient =
CGGradientCreateWithColorComponents(rgbColorspace, components, locations, num_locations);
CGPoint topCenter = CGPointMake(25, 20);
CGPoint midCenter = CGPointMake(25, 25);
CGContextDrawRadialGradient(theContext, glossGradient, midCenter, 20, topCenter, 10, kCGGradientDrawsAfterEndLocation);
现在真正奇怪的是,如果我稍微修改绘图代码,只绘制普通的红色圆圈,如下所示:
- (void)drawInContext:(CGContextRef)theContext{
CGContextSetAllowsAntialiasing(theContext, true);
CGContextSetShouldAntialias(theContext, true);
CGContextSetFillColorWithColor(theContext, [UIColor redColor].CGColor);
CGContextFillEllipseInRect(theContext, CGRectMake(0, 0, 40,40));
}
一切似乎都进行了抗锯齿处理:
http://www.expensivedna.com/IMG_0017.PNG http://www.expensivedna.com/IMG_0017.PNG
我似乎无法理解这种看似奇怪的行为。我是否错过了渐变和普通圆之间的某些区别?
谢谢大家。