【发布时间】:2010-09-23 12:52:58
【问题描述】:
我正在绘制一个饼图,每个切片都有不同的颜色。我需要让切片看起来有质感,而不仅仅是纯色。任何想法如何做到这一点?我不想使用图像作为所有可能颜色的纹理。所以我需要生成纹理或类似的东西。有任何想法吗。谢谢你! ps:这是一个iphone项目。 (我不能使用Core Image)
【问题讨论】:
标签: iphone
我正在绘制一个饼图,每个切片都有不同的颜色。我需要让切片看起来有质感,而不仅仅是纯色。任何想法如何做到这一点?我不想使用图像作为所有可能颜色的纹理。所以我需要生成纹理或类似的东西。有任何想法吗。谢谢你! ps:这是一个iphone项目。 (我不能使用Core Image)
【问题讨论】:
标签: iphone
将 colorWithPatternImage 与 UIColor 一起使用。
编辑:抱歉应该正确阅读问题。
您需要使用 UIGraphicsContext 来创建可以在 colorWithPatternImage 中使用的图像。我建议使用可以加载的灰度图像tint with a similar method to this,然后在 UIColor 中用作图案。
所以你会有一个类似这样的方法:
- (UIColor *)texturedPatternWithTint:(UIColor *)tint {
UIImage *texture = [UIImage imageNamed:@"texture.png"];
CGRect wholeImage = CGRectMake(0, 0, texture.size.width, texture.size.height);
UIGraphicsBeginImageContextWithOptions(texture.size, NO, 0.0);
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextDrawImage(context, wholeImage, texture.CGImage);
CGContextSetBlendMode(context, kCGBlendModeMultiply);
CGContextSetFillColor(context, CGColorGetComponents(tint.CGColor));
CGContextFillRect(context, self.bounds);
UIImage *tintedTexture = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return [UIColor colorWithPatternImage:tintedTexture];
}
(未测试)
【讨论】: