Objective C: Using UIImage for Stroking

依然范特西╮ 提交于 2019-12-03 22:14:12

问题


I am trying to apply a texture for my brush but i'm really having a hard time figuring how it is done.

Here's the image of my output.

I used an UIImage that just follows the touch on the screen but when i swipe it faster the result is on the right side "W", and on the left side that's the result when i swipe it slow.

i tried using the CGContextMoveToPoint and CGContextAddLineToPoint i don't know how apply the texture.

Is it possible to use UIImage for the stroke texture?

Here's my code

    UIImage * brushImageTexture = [UIImage imageNamed:@"brushImage.png"]; 
    [brushImagetexture drawAtPoint:CGPointMake(touchCurrentPosition.x, touchVurrentPosition.y) blendMode:blendMode alpha:1.0f];

回答1:


You need to manually draw the image at each point along the line from the previous point to the current point.

CGPoint vector = CGPointMake(currentPosition.x - lastPosition.x, currentPosition.y - lastPosition.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(lastPosition.x + i * vector.x, lastPosition.y + i * vector.y);
    [brushImageTexture drawAtPoint:p blendMode:blendMode alpha:1.0f];
}


来源:https://stackoverflow.com/questions/8290680/objective-c-using-uiimage-for-stroking

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!