2011-07-19 17 views
0

私はiphone開発に新規で、quartz2Dを使用して線を引くことができます。
画像(画像)の上に線を引く機能を実現したいと考えています。たとえば、画像名がmap.jpg(追加する画像ビューを使用)です。画像(画像)の上に線を引く問題

詳しいコードや方法を教えていただけますか?前もって感謝します!

コード:

- (void)drawRect:(CGRect)rect 

答えて

1

は、線を引くために、このコードを使用します。

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event 
{ 

     mouseSwiped = NO; 

     UITouch *touch = [touches anyObject]; 

     if ([touch tapCount] == 2) { 

      drawImage.image = nil; 

      return; 

     } 

     lastPoint = [touch locationInView:self.view]; 

     lastPoint.y -= 20; 

} 

- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event 
{ 

     mouseSwiped = YES; 

     UITouch *touch = [touches anyObject]; 

     CGPoint currentPoint = [touch locationInView:self.view]; 

     currentPoint.y -= 20; 


     UIGraphicsBeginImageContext(self.view.frame.size); 

     [drawImage.image drawInRect:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)]; 

     CGContextSetLineCap(UIGraphicsGetCurrentContext(), kCGLineCapRound); 

     CGContextSetLineWidth(UIGraphicsGetCurrentContext(), 5.0); 

     CGContextSetRGBStrokeColor(UIGraphicsGetCurrentContext(), 0, 0, 0, 1); 

     CGContextBeginPath(UIGraphicsGetCurrentContext()); 

     CGContextMoveToPoint(UIGraphicsGetCurrentContext(), lastPoint.x, lastPoint.y); 

     CGContextAddLineToPoint(UIGraphicsGetCurrentContext(), currentPoint.x, currentPoint.y); 

     CGContextStrokePath(UIGraphicsGetCurrentContext()); 

     drawImage.image = UIGraphicsGetImageFromCurrentImageContext(); 

     UIGraphicsEndImageContext(); 

     lastPoint = currentPoint; 

     mouseMoved++; 

     if (mouseMoved == 10) { 

      mouseMoved = 0; 

     } 

} 
+0

本当にありがとうございます。このメソッドを使用して、画像(画像)の上に線を描画する関数( - (void)drawRect:(CGRect)rect)を実現したいと考えています。 \t CGContextSetLineWidth(context、1.0); \t CGColorSpaceRef colorspace = CGColorSpaceCreateDeviceRGB(); \t CGFloatコンポーネント[] = {1.0,0.0,0.0,1.0}; \t CGColorRef color = CGColorCreate(colorspace、components); \t CGContextSetStrokeColorWithColor(context、color); \t CGContextMoveToPoint(context、50、50); \t CGContextAddLineToPoint(context、200、200); \t CGContextStrokePath(context); \t CGColorSpaceRelease(colorspace); \t CGColorRelease(color); – tianke