gpt4 book ai didi

iphone - 使用核心图形 : bubbles are shown 绘制线条时出现问题

转载 作者:太空狗 更新时间:2023-10-30 03:26:07 26 4
gpt4 key购买 nike

使用 core graphics ,我试图画一条线,因为当我尝试添加阴影时会在线内创建 lin 和气泡,看图像,我无法解决问题。请在下面找到我的代码

-(void)drawRect:(CGRect)rect
{

[curImage drawAtPoint:CGPointMake(0, 0)];
CGPoint mid1 = midPoint(previousPoint1, previousPoint2);
CGPoint mid2 = midPoint(currentPoint, previousPoint1);

CGContextRef context = UIGraphicsGetCurrentContext();

[self.layer renderInContext:context];

CGContextMoveToPoint(context, mid1.x, mid1.y);
CGContextAddQuadCurveToPoint(context, previousPoint1.x, previousPoint1.y, mid2.x, mid2.y);
CGContextSetLineCap(context, kCGLineCapRound);
CGContextSetLineWidth(context, self.lineWidth);
CGContextSetStrokeColorWithColor(context, self.lineColor.CGColor);

CGContextSetShadow(context, CGSizeMake(-16.00, -5.0f), 5.0f);

CGContextStrokePath(context);

[super drawRect:rect];

[curImage release];

}

下面是touchesMoved方法。

-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{

UITouch *touch = [touches anyObject];

previousPoint2 = previousPoint1;
previousPoint1 = [touch previousLocationInView:self];
currentPoint = [touch locationInView:self];

// calculate mid point
CGPoint mid1 = midPoint(previousPoint1, previousPoint2);
CGPoint mid2 = midPoint(currentPoint, previousPoint1);

CGMutablePathRef path = CGPathCreateMutable();
CGPathMoveToPoint(path, NULL, mid1.x, mid1.y);
CGPathAddQuadCurveToPoint(path, NULL, previousPoint1.x, previousPoint1.y, mid2.x, mid2.y);
CGRect bounds = CGPathGetBoundingBox(path);
CGPathRelease(path);

CGRect drawBox = bounds;

//Pad our values so the bounding box respects our line width
drawBox.origin.x -= self.lineWidth * 2;
drawBox.origin.y -= self.lineWidth * 2;
drawBox.size.width += self.lineWidth * 4;
drawBox.size.height += self.lineWidth * 4;

UIGraphicsBeginImageContext(drawBox.size);
[self.layer renderInContext:UIGraphicsGetCurrentContext()];
curImage = UIGraphicsGetImageFromCurrentImageContext();
[curImage retain];
UIGraphicsEndImageContext();
[self setNeedsDisplayInRect:drawBox];

}

CGPoint midPoint(CGPoint p1, CGPoint p2)
{
return CGPointMake((p1.x + p2.x) * 0.5, (p1.y + p2.y) * 0.5);
}


-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{

UITouch *touch = [touches anyObject];

previousPoint1 = [touch previousLocationInView:self];
previousPoint2 = [touch previousLocationInView:self];
currentPoint = [touch locationInView:self];

[self touchesMoved:touches withEvent:event];
}

当我尝试添加行时,问题就来了

CGContextSetShadow(context, CGSizeMake(-16.00, -5.0f), 5.0f); 

Issue image

请帮我解决这个问题,在此先感谢。请参阅使用方法编辑问题。

最佳答案

看起来您正在将一系列线段绘制为四边形曲线。气泡将位于前一段和当前段重叠的位置。由于您的分段线是纯红色,您不会注意到重叠,但它在阴影中显示为较暗的区域。

尝试将描边颜色设置为 alpha 为 0.5(或类似的半透明)。我想(知道)你会看到重叠的部分也会显示出与阴影类似的效果。

为了解决这个问题,您需要将线段绘制为每条线的连续路径。我猜你正在使用 touchesBegan:withEvent:/touchesMoved:withEvent/touchesEnded:withEvent: 来获取线条的点?

在这种情况下,当调用 touchesBegan:withEvent: 时,开始您的新路径。在 touchesMoved:withEvent 期间,在当前图像上渲染新路径。在 touchesEnded:withEvent: 中提交 UIImage 的路径。此时您可以丢弃路径,直到 touchesBegan:withEvent: 被下一个笔划再次调用。

更新

在您的代码片段中,您最终会在每个运行循环中渲染整个 View 3 次。这是很多绘图开销。我快速整理了一些代码来演示我上面所说的内容。它不是经过测试的代码,但它应该大部分是正确的:

更新 2

根据我最近的两条评论,我花了一些时间来编写和测试一些代码,这些代码可以满足您的需求。这段代码来自 View Controller 而不是 View 本身,但稍作修改,您可以根据需要在 View 中移动。因此,我添加了一个 UIImageView 作为通用 UIView 的 subview 来保存和显示图像(并在 View Controller 中维护对的引用),并创建 View Controller 是第一响应者,以便接收触摸事件。在集成到您自己的项目中时,您可以清理大量硬编码值:

static
UIImage *CreateImage( CGRect rect, NSArray *paths )
{
UIGraphicsBeginImageContextWithOptions(rect.size, NO, 1.0);
CGContextRef context = UIGraphicsGetCurrentContext();

CGFloat colorComponents[4] = {0.0,0.0,0.0,0.0};
CGContextSetFillColor(context, colorComponents);
CGContextFillRect(context, rect);

for ( id obj in paths ) {
CGPathRef path = (CGPathRef)obj;
CGContextAddPath(context, path);
}
CGContextSetStrokeColorWithColor(context, [[UIColor redColor] CGColor]);
CGContextSetLineCap(context, kCGLineCapRound);
CGContextSetLineWidth(context, 5.0);
CGContextSetShadow(context, (CGSize){-8.0,40}, 5.0);
CGContextBeginTransparencyLayer(context, NULL);

CGContextDrawPath(context, kCGPathStroke);

CGContextEndTransparencyLayer(context);

UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return image;
}

static
CGPoint MidPoint( CGPoint pt1, CGPoint pt2 )
{
return (CGPoint){(pt1.x+pt2.x)/2.0,(pt1.y+pt2.y)/2.0};
}

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
[super touchesBegan:touches withEvent:event];
CGPoint location = [[touches anyObject] locationInView:self.view];
myPath = CGPathCreateMutable();
CGPathMoveToPoint(myPath, NULL, location.x, location.y);

[self.paths addObject:(id)myPath];
}

- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
[super touchesMoved:touches withEvent:event];
UITouch *touch = [touches anyObject];
CGPoint previousLocation = [touch previousLocationInView:self.view];
CGPoint location = [touch locationInView:self.view];
CGPoint midPoint = MidPoint(previousLocation, location);
CGPathAddQuadCurveToPoint(myPath, NULL, midPoint.x, midPoint.y, location.x, location.y);

self.imageView.image = CreateImage(self.view.bounds, self.paths);
}

- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
[super touchesEnded:touches withEvent:event];
UITouch *touch = [touches anyObject];
CGPoint previousLocation = [touch previousLocationInView:self.view];
CGPoint location = [touch locationInView:self.view];
CGPoint midPoint = MidPoint(previousLocation, location);
CGPathAddQuadCurveToPoint(myPath, NULL, midPoint.x, midPoint.y, location.x, location.y);

self.imageView.image = CreateImage(self.view.bounds, self.paths);

CGPathRelease(myPath);
myPath = nil;
}

关于iphone - 使用核心图形 : bubbles are shown 绘制线条时出现问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11115909/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com