【问题标题】:create cgpath out of drawRect从 drawRect 创建 cgpath
【发布时间】:2012-03-07 17:11:07
【问题描述】:

我有 UIView 子类,我想从 drawRect 中绘制一个 CGPath。我听说这是不可能的。我知道 CGPath 需要上下文。我试图用 newImage 给它上下文。我还认为我必须以不同的方式调用它,例如我在 CCLayer 中的代码,以便视图显示 drawRect 中的内容

- (void)createPath:(CGPoint)origin
{
CGSize size=CGSizeMake(320, 480);
CGContextRef context = UIGraphicsGetCurrentContext();
UIGraphicsBeginImageContextWithOptions(size,YES,1.0f);
CGContextSaveGState(context);

UIImage *myImage = UIGraphicsGetImageFromCurrentImageContext();


CGRect newRect= CGRectMake(0, 0, 320, 480);
CGMutablePathRef path1 = CGPathCreateMutable();

CGPathMoveToPoint(path1, nil,100, 200);

CGPoint newloc = CGPointMake(300, 130);

CGPathMoveToPoint(path1, NULL, newloc.x, newloc.y);
CGPathAddLineToPoint(path1, NULL, newloc.x + 16,newloc.y + 38);
CGPathAddLineToPoint(path1, NULL, newloc.x + 49, newloc.y + 0);
CGPathAddLineToPoint(path1, NULL, newloc.x + 23,  newloc.y - 39);
CGPathAddLineToPoint(path1, NULL, newloc.x - 25,newloc.y - 40);
CGPathAddLineToPoint(path1, NULL, newloc.x -43, newloc.y + 0);
CGPathCloseSubpath(path1);

CGContextAddPath(context, path1);

CGContextSaveGState(context);
CGContextSetLineWidth(context, 10.0);
CGContextSetStrokeColorWithColor(context, [UIColor orangeColor].CGColor);
CGContextSetFillColorWithColor(context, [UIColor blackColor].CGColor);
//Fill and stroke
CGContextDrawPath(context, kCGPathFillStroke);
//CGContextDrawImage(context, newRect, myImage.CGImage);

UIGraphicsEndImageContext();
CGContextRestoreGState(context);
CGPathRelease(path1); 
CGContextRelease(context);

}

【问题讨论】:

  • 您是否希望将图像从路径绘制的上下文中取出?
  • 我只是想让 CGPath 看起来像一条小路。当我在drawRect中做所有事情时,我知道该怎么做。我不知道如何在 drawRect 之外绘制 CGPath。我知道我需要上下文,并且我认为我需要制作一个图像来绘制。

标签: iphone calayer drawrect cgpath


【解决方案1】:

看看这对你有没有帮助。有一个 UIView 和 UIImageView 作为它的子视图。然后实现 touches 方法如下:

- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event 
{
    UITouch *touch = [touches anyObject];   

    //Get touch point
    CGPoint currentPoint = [touch locationInView:drawImage];

    UIGraphicsBeginImageContext(self.frame.size);

    //drawImage is the UIImageView
    [drawImage.image drawInRect:CGRectMake(0, 0, drawImage.frame.size.width, drawImage.frame.size.height)]; 

    //Line attributes
    CGContextSetLineCap(UIGraphicsGetCurrentContext(), kCGLineCapRound);

    CGContextSetLineWidth(UIGraphicsGetCurrentContext(), 10.0);

    CGContextSetRGBStrokeColor(UIGraphicsGetCurrentContext(), 1.0, 0.5, 1.0, 1.0);

    //Begin path
    CGContextBeginPath(UIGraphicsGetCurrentContext());

    CGPathMoveToPoint(drawPath, NULL, lastPoint.x, lastPoint.y);

    CGPathAddLineToPoint(drawPath, NULL, currentPoint.x, currentPoint.y);

    CGContextMoveToPoint(UIGraphicsGetCurrentContext(), lastPoint.x, lastPoint.y);
    CGContextAddLineToPoint(UIGraphicsGetCurrentContext(), currentPoint.x, currentPoint.y);
    CGContextStrokePath(UIGraphicsGetCurrentContext());

    drawImage.image = UIGraphicsGetImageFromCurrentImageContext();

    UIGraphicsEndImageContext();
}

现在您可以看到 UIImageView 与您绘制的内容和 drawPath 将有您的路径。

【讨论】:

  • 如何制作 UIImageView? drawInRect 方法有什么意义(为了知识)?
  • 我想出了我评论的第一部分(上图)。但是我不确定一般的解决方案。这就是我所拥有的......有UIView。我有一个 CCLayer,我在其中添加 UIView 来显示它。我有一个 createPath 方法。然后我在方法中创建一个 CGPath。该方法包含您的代码。我没有任何显示。你在drawInRect中制作CGPath吗?次要问题。您创建的方法会在 CCLayer 或 ViewController 中绘制,而 drawInRect 将是我的 UIView 方法中的方法吗?你能解释一下你的代码属于哪里以及为什么。我很笨。
  • 这段代码属于一个 UIView 类。此类又具有 UIImageView 子视图。触摸方法用于捕获路径。我使用图像视图来显示已完成的涂鸦。
  • 好的,感谢您的帮助,我找到了我需要做的事情。有时我无法在我面前看到答案。我忘了添加 [self addSubview:drawImage];
  • 没问题。不要忘记勾选我的答案!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2014-08-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-10-22
  • 2015-08-11
相关资源
最近更新 更多