UIColor的纹理?

时间:2010-09-23 12:52:59

标签: iphone

我正在绘制饼图,每个切片都有不同的颜色。我需要给切片一个纹理外观,而不仅仅是纯色。任何想法如何做到这一点?我不想使用图像作为所有可能颜色的纹理。所以我需要生成一个纹理或类似的东西。有任何想法吗。谢谢! ps:这是一个iphone项目。 (我不能使用Core Image)

1 个答案:

答案 0 :(得分:4)

将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];
}

(未经测试)