使用CoreGraphics中的图像描画CGContext路径 [英] Stroke a CGContext path using an image in CoreGraphics
问题描述
我正在尝试通过 CoreGraphics
框架通过抚摸图像来获得类似画笔的效果来画线。
Im trying to draw line by stroking an image to have brush like effect via CoreGraphics
framework.
I关注了这篇文章和这篇文章,并尝试从 touchesMoved中提取
方法和代码示例的形式为
I followed this post and this post and tried to draw from the touchesMoved
method and the code sample'll be of the form
UITouch *touch = [touches anyObject];
CGPoint currentPoint = [touch locationInView:self.view];
UIImage *texture = [UIImage imageNamed:@"texture.png"];
CGPoint vector = CGPointMake(currentPoint.x - lastPoint.x, currentPoint.y - lastPoint.y);
NSLog(@" vector %@", NSStringFromCGPoint(vector));
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(lastPoint.x + i * vector.x, lastPoint.y + i * vector.y);
[texture drawAtPoint:p blendMode:kCGBlendModeNormal alpha:1.0f];
NSLog(@"p %@", NSStringFromCGPoint(p));
}
previousPoint = currentPoint;
这样做,我收到了错误消息,
By doing so, I got error msgs,
Jul 9 11:51:27 mac1.local Smooth Line View[1035] <Error>: CGContextSaveGState: invalid context 0x0
Jul 9 11:51:27 mac1.local Smooth Line View[1035] <Error>: CGContextSetBlendMode: invalid context 0x0
Jul 9 11:51:27 mac1.local Smooth Line View[1035] <Error>: CGContextSetAlpha: invalid context 0x0
Jul 9 11:51:27 mac1.local Smooth Line View[1035] <Error>: CGContextTranslateCTM: invalid context 0x0
Jul 9 11:51:27 mac1.local Smooth Line View[1035] <Error>: CGContextScaleCTM: invalid context 0x0
Jul 9 11:51:27 mac1.local Smooth Line View[1035] <Error>: CGContextDrawImage: invalid context 0x0
Jul 9 11:51:27 mac1.local Smooth Line View[1035] <Error>: CGContextRestoreGState: invalid context 0x0
放在与上下文相关的代码上,修改后的代码为
So, I put on the code related for context and the modified code is
UITouch *touch = [touches anyObject];
currentPoint = [touch locationInView:self];
UIGraphicsBeginImageContext(self.frame.size);
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextSetBlendMode(context, kCGBlendModeNormal);
CGContextSetAlpha(context, 1.0f);
CGContextSaveGState(context);
UIImage *texture = [UIImage imageNamed:@"textureImg.png"];
[texture drawAtPoint:currentPoint blendMode:kCGBlendModeNormal alpha:1.0f];
CGContextSetLineCap(context, kCGLineCapRound);
CGContextSetLineWidth(context, 25.0);
CGContextSetRGBStrokeColor(context, texture.CGColor);
CGContextBeginPath(context);
CGContextMoveToPoint(context, previousPoint.x, previousPoint.y);
CGContextAddLineToPoint(context, currentPoint.x, currentPoint.y);
CGContextDrawPath(context, kCGPathFillStroke);
UIGraphicsEndImageContext();
但是当我跑步并尝试进行触摸动作时,什么也没画。我无法理解出了什么问题。有人可以指出我的错误吗?我仍然花很多时间来解决这个问题,但现在还无法完成。任何及时的帮助深表感谢。
谢谢。
But when I run and try to draw on touch move, nothing is drawn. I cant get what went wrong. Can someone point me my mistake? Im still spending much time in sorting this out, but couldn't do it yet now. Any timely help is much appreciated. Thanks.
推荐答案
我知道,您的touchesMoved应该看起来像这样。
I see, your touchesMoved should look like this.
BOOL touchSwiped = YES;
UITouch *touch = [touches anyObject];
currentPoint = [touch locationInView:self.view];
currentPoint.y -= 685; //<
//currentPoint.x = ???; <------ adjust the x and y points of the currentPoint if it is not on the exact point of drawing.
UIGraphicsBeginImageContext(touchDraw.frame.size);
[touchDraw.image drawInRect:CGRectMake(0, 0, touchDraw.frame.size.width, touchDraw.frame.size.height)];
CGContextSetLineCap(UIGraphicsGetCurrentContext(), kCGLineCapRound);
UIImage * textureColor = [UIImage imageNamed:@"brushImg.png"];
CGPoint vector = CGPointMake(currentPoint.x - previousPoint.x, currentPoint.y - previousPoint.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(previousPoint.x + i * vector.x, previousPoint.y + i * vector.y);
[textureColor drawAtPoint:p blendMode:blendMode alpha:0.5f];
}
CGContextBeginPath(UIGraphicsGetCurrentContext());
CGContextStrokePath(UIGraphicsGetCurrentContext());
touchDraw.image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
previousPoint = currentPoint;
touchMoved++;
if (touchMoved == 1) {
touchMoved = 0;
}
这篇关于使用CoreGraphics中的图像描画CGContext路径的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!