在图像(图片)问题上方画一条线

时间:2011-07-19 06:17:01

标签: iphone line quartz-2d

我是iphone开发的新手,可以使用quartz2D使用下面的代码画一条线 我想实现在图片(图片)上方画一条线的功能,例如我有一个名为map.jpg的地图图像(使用imageview添加)。

你能告诉我详细的代码或方法吗?提前谢谢!

代码:

- (void)drawRect:(CGRect)rect 

1 个答案:

答案 0 :(得分: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;

      } 

}