2012-03-07 6 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を持っています。今、あなたはあなたが描いたとdrawPathがパスを持っています何と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(); 
} 

:以下のようにタッチメソッドを実装します。

+0

UIImageViewはどのように作成しますか? drawInRectメソッドのポイントは何ですか(知識のため)? – Asdrubal

+0

私はコメントの最初の部分(上記)を理解しました。しかし、私は一般的な解決策がわかりません。ここに私が持っているものは... UIViewがあります。私はそれを表示するためにUIViewを追加するCCLayerを持っています。私はcreatePathメソッドを持っています。私はその方法でCGPathを作成します。このメソッドにはコードが含まれています。私は何も示していない。あなたはdrawInRectでCGPathを作っていますか? 2次質問です。作成したメソッドをCCLayerまたはViewControllerに描画し、drawInRectをUIViewメソッドのメソッドにしますか?コードの所在や理由を説明できますか?私は非常に愚かです。 – Asdrubal

+0

このコードはUIViewクラスに属します。このクラスにはUIImageViewサブビューがあります。タッチメソッドは、パスをキャプチャするために使用されます。私は落書きを表示するために画像ビューを使用します。 – cocoakomali

関連する問題