【问题标题】:Texture from UIColor?来自 UIColor 的纹理?
【发布时间】:2010-09-23 12:52:58
【问题描述】:

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

【问题讨论】:

    标签: iphone


    【解决方案1】:

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

    (未测试)

    【讨论】:

    • 不能很好地工作。它不与颜色混合,只是显示纹理。能给我一张纹理图吗?
    猜你喜欢
    • 1970-01-01
    • 2013-01-25
    • 2011-08-12
    • 1970-01-01
    • 2012-06-15
    • 2013-03-30
    • 2013-12-25
    • 1970-01-01
    • 2020-12-25
    相关资源
    最近更新 更多