开发者

Texture from UIColor?

开发者 https://www.devze.com 2023-01-17 02:40 出处:网络
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 textu

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开发者_JS百科't use Core Image)


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)

0

精彩评论

暂无评论...
验证码 换一张
取 消