Stroking paths using a pattern color to simulate b

2019-03-09 14:06发布

问题:

I am making a finger painting app and I am having a hard time giving my brush a nice brush-like feel and texture.

I have this code:

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

but my output is like this:

The texture of my brush is tiled. How can I make it fit to the stroke Size and stop the tiling of the image?

回答1:

Here i found a solution!

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