2012-03-07 3 views
2

저는 UIView 하위 클래스가 있고 drawRect 밖으로 CGPath를 그립니다. 나는 그것이 가능하고 불가능하다는 소식을 들었다. CGPath에 문맥이 필요하다는 것을 이해합니다. newImage에 컨텍스트를 지정하려고했습니다. 나는 또한이 당신을 도움이된다면 참조의 drawRectdrawRect 밖으로 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

+0

난 그냥 흔적처럼 보이게 CGPath를 만들고 싶다. drawRect에서 모든 것을 할 때 어떻게하는지 알 수 있습니다. DrawRect 밖에서 CGPath를 그리는 방법을 모르겠습니다. 저는 컨텍스트가 필요하다는 것을 압니다. 그리기 위해 이미지를 만들어야한다고 생각했습니다. – Asdrubal

답변

0

에있을 것입니다 무엇을 표시 할 뷰의 CCLayer 예를 들면 다르게 내 코드를이 전화를해야한다는 생각입니다. UIImageView와 UIView를 하위 뷰로 사용하십시오. 그런 다음 아래와 같이 터치 방식을 구현 :

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

지금 당신이 그린과 drawPath 당신의 경로가 무슨와 함께있는 UIImageView를 볼 수 있습니다.

+0

UIImageView는 어떻게 만들 수 있습니까? drawInRect 메서드의 요점은 무엇입니까 (지식을 위해서)? – Asdrubal

+0

나는 (위) 내 의견의 첫 부분을 알아 냈습니다. 그러나 나는 일반적인 해결책을 확신하지 못한다. 다음은 내가 가지고있는 것입니다 ... UIView가 있습니다. 나는 그것을 표시하는 UIView를 추가 CCLayer있다. createPath 메서드가 있습니다. 그런 다음 메서드에 CGPath를 만듭니다. 메서드에 코드가 포함되어 있습니다. 나는 아무것도 보이지 않는다. drawInRect에서 CGPath를 만들고 있습니까? 2 차 질문입니다. 당신이 만든 메서드가 CCLayer 또는 ViewController에 그려지고 drawInRect가 내 UIView 메서드의 메서드가 될까요? 코드가 어디에 있고 왜 있는지를 설명해 주시겠습니까? 나는 매우 바보입니다. – Asdrubal

+0

이 코드는 UIView 클래스에 속합니다. 이 클래스에는 차례로 UIImageView 하위 뷰가 있습니다. 터치 방법은 경로를 캡처하는 데 사용됩니다. 나는 이미지보기를 사용하여 낙서를 표시합니다. – cocoakomali