2012-03-07 63 views
2

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

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

} 
+0

您是否希望将图像从路径绘制的上下文中移出? – cocoakomali 2012-03-07 19:03:49

+0

我只是想让CGPath看起来像一条线索。当我在drawRect中完成所有事情时,我知道该怎么做。我不知道如何在drawRect之外绘制CGPath。我知道我需要上下文,并且我认为我需要制作一个图像来绘制。 – Asdrubal 2012-03-07 21:13:48

回答

0

看看这可以帮助你。有UIImageView作为它的子视图。然后实现触摸方法如下:

- (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将有你的路径可见。

+0

我将如何制作UIImageView? drawInRect方法的意义是什么(为了知识)? – Asdrubal 2012-03-12 16:29:49

+0

我想出了我评论的第一部分(上图)。但我不确定一般的解决方案。这是我有... 有UIView。我有一个CCLayer,我添加了UIView来显示它。我有一个createPath方法。然后我在该方法中创建一个CGPath。该方法包含您的代码。我没有显示任何东西。你在drawInRect中创建CGPath吗? 第二个问题。您创建的方法会在CCLayer或ViewController中绘制,并且drawInRect会是我UIView方法中的一个方法吗?你能否解释你的代码属于哪里以及为什么。我很愚蠢。 – Asdrubal 2012-03-12 16:47:51

+0

此代码属于UIView类。这个类又有一个UIImageView子视图。触摸方法用于捕获路径。我使用图像视图来显示已完成的涂鸦。 – cocoakomali 2012-03-12 18:04:13