2010-09-23 52 views
2

我正在繪製一個餅圖,每個切片都有不同的顏色。我需要給這些切片帶來紋理外觀,而不僅僅是簡單的顏色。任何想法如何做到這一點?我不想使用圖像作爲所有可能顏色的紋理。所以我需要生成一個紋理或類似的東西。有任何想法嗎。謝謝! ps:這是一個iphone項目。 (我無法使用Core Image)UIColor的紋理?

回答

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

(未測試)

+0

不很好地工作。它不會與僅顯示紋理的顏色混合。你能給我一個紋理圖像嗎? – user426132 2010-09-23 14:39:39