ios - 在 CoreGraphics 中使用图像描边 CGContext 路径

标签 ios core-graphics cgcontext quartz-core

我正在尝试通过 CoreGraphics 框架通过抚摸图像来绘制类似画笔的效果。

我关注了this postthis post并尝试从 touchesMoved 方法中提取,代码示例的形式为

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;

通过这样做,我得到了错误消息,

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

所以,我把上下文相关的代码放在上面,修改后的代码是

    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();

但是当我运行并尝试在触摸移动时绘制时,没有绘制任何东西。我不知道出了什么问题。有人可以指出我的错误吗?我还在花很多时间来解决这个问题,但现在还做不到。非常感谢任何及时的帮助。 谢谢。

最佳答案

我明白了,你的 touchesMoved 应该是这样的。

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

关于ios - 在 CoreGraphics 中使用图像描边 CGContext 路径,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11390079/

相关文章:

ios - 如何在 iOS 7 中创建背景为 "blurred"的透视按钮

ios - 在 iOS 8 通知中心小部件中使用 Parse.com 数据

ios4 - 有什么方法可以在 Objective-C/iPhone/iOS SDK 中禁用 CALayer 的 rasterizationScale 插值/抗锯齿功能吗?

iphone - CTFrameGetLineOrigins 中的错误?

iphone - 通过在后台线程上渲染 UIWebView 创建 UIImage - iPhone

ios - AutoLayout:UIImageView 和 Aspect Fit 内容模式

ios - 在 swift 3 中,按钮单击应用程序崩溃,与 Storyboard绑定(bind)

ios - 如何释放 ARC 中的内存以进行高内存使用图形渲染?

ios - objective c renderInContext 在后台线程上崩溃