2

I am drawing a pie chart, each slice has a different color. I need to give the slices a textured look, not just the plain color. Any ideas how to do this? I don't want to use a image to use as a texture for all the possible colors. So I need to generate a texture or something like that. Any ideas. Thank You! ps: this is an iphone project. (I can't use Core Image)

user426132
  • 1,341
  • 4
  • 13
  • 28

1 Answers1

4

Use colorWithPatternImage with UIColor.

Edit: Sorry should have read the question properly.

You will need to use a UIGraphicsContext to create an image you can use in colorWithPatternImage. I would suggest using a grayscale image that you can load in, tint with a similar method to this, then use as a pattern in UIColor.

So you would have a method along the lines of this:

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

(not tested)

Community
  • 1
  • 1
Alastair Stuart
  • 4,165
  • 3
  • 36
  • 33
  • Doesn't work very well. It does not blend with the color just the texture is shown. Can you give me a texture image too? – user426132 Sep 23 '10 at 14:39