2012-03-07 64 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