使用图案颜色描边路径以模拟画笔纹理

时间:2011-11-23 04:13:43

标签: objective-c cocoa-touch drawing core-graphics

我正在制作一个手指画应用程序,我很难给我的笔刷一个漂亮的笔刷般的感觉和纹理。

我有这段代码:

    UIColor * brushTexture = [UIColor colorWithPatternImage:[UIImage imageNamed:@"Brush_Black.png"]]; 
    CGContextSetStrokeColorWithColor(UIGraphicsGetCurrentContext(), brushTexture.CGColor); 

但我的输出是这样的:
Image of Mouse

我的画笔纹理是平铺的。如何使其适合笔划大小并停止图像的平铺?

1 个答案:

答案 0 :(得分:7)

我在这里找到了解决方案!

    UIImage *texture = [UIImage imageNamed:"brush.png"]    
    CGPoint vector = CGPointMake(currentPoint.x - endPoint.x, currentTPoint.y - endPoint.y);
    CGFloat distance = hypotf(vector.x, vector.y);
    vector.x /= distance;
    vector.y /= distance;
    for (CGFloat i = 0; i < distance; i += 1.0f) {
        CGPoint p = CGPointMake(endPoint.x + i * vector.x, endPoint.y + i * vector.y);
        [texture drawAtPoint:p blendMode:blendMode alpha:0.5f];
    }