2011-07-19 33 views
0

我是iphone開發新手,可以使用下面的代碼使用quartz2D來畫一條線。
我想實現在圖片(圖片)上方繪製線條的功能,例如我有一個地圖圖片名稱爲:map.jpg(使用imageview添加)。在圖片(圖片)上面畫一條線問題

你能告訴我詳細的代碼或方法嗎?提前致謝!

代碼:

- (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)矩形),它在圖片(圖片)上繪製一條線。 \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