2013-04-18 69 views
0

我已经搜索了很多关于这个问题的答案,但是他们没有一个看起来像我想要的那样。很多教程向我展示了如何在代码中添加线条和多边形,但不适用于徒手画。通过MKMap在UIImageView上绘图查看

我在地图上得到了一个带有UIImageView的MKMapView。而且我可以在我的UIImageView上画一点,但之后,MKMapView被拖拽并且绘图不会继续。

我想知道我做错了什么?

这里是我的触摸事件的代码:

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event { 
    UITouch *touch = [touches anyObject]; 
    pointA = [touch locationInView:drawView]; 
} 

- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event { 
     UITouch *touch = [touches anyObject]; 
     CGPoint currentLocation = [touch locationInView:drawView]; 

     UIGraphicsBeginImageContext(drawView.frame.size); 
     CGContextRef ctx = UIGraphicsGetCurrentContext(); 
     [drawView.image drawInRect:CGRectMake(0, 0, drawView.frame.size.width, drawView.frame.size.height)]; 
     CGContextSetLineCap(ctx, kCGLineCapRound); 
     CGContextSetLineWidth(ctx, 5.0); 
     CGContextSetRGBStrokeColor(ctx, 1.0, 0.0, 0.0, 1.0); 
     CGContextBeginPath(ctx); 
     CGContextMoveToPoint(ctx, pointA.x, pointA.y); 
     CGContextAddLineToPoint(ctx, currentLocation.x, currentLocation.y); 
     CGContextStrokePath(ctx); 
     drawView.image = UIGraphicsGetImageFromCurrentImageContext(); 
     UIGraphicsEndImageContext(); 

     pointA = currentLocation; 
} 

- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event { 
     UITouch *touch = [touches anyObject]; 
     CGPoint currentLocation = [touch locationInView:drawView]; 

     UIGraphicsBeginImageContext(drawView.frame.size); 
     CGContextRef ctx = UIGraphicsGetCurrentContext(); 
     [drawView.image drawInRect:CGRectMake(0, 0, drawView.frame.size.width, drawView.frame.size.height)]; 
     CGContextSetLineCap(ctx, kCGLineCapRound); 
     CGContextSetLineWidth(ctx, 5.0); 
     CGContextSetRGBStrokeColor(ctx, 1.0, 0.0, 0.0, 1.0); 
     CGContextBeginPath(ctx); 
     CGContextMoveToPoint(ctx, pointA.x, pointA.y); 
     CGContextAddLineToPoint(ctx, currentLocation.x, currentLocation.y); 
     CGContextStrokePath(ctx); 
     drawView.image = UIGraphicsGetImageFromCurrentImageContext(); 
     UIGraphicsEndImageContext(); 

     pointA = currentLocation; 
} 

而且按钮的代码谁显示的UIImageView:

编辑:我做了这个用于回采与地图的互动:

- (IBAction)modeDraw:(id)sender { 
    if (drawView.alpha == 0.0) { 
     drawView.image=nil; //empty for a new draw 
     drawView.backgroundColor = [UIColor whiteColor]; 
     [UIImageView beginAnimations:nil context:nil]; 
     [UIImageView setAnimationDuration:0.5]; 
     [drawView setAlpha:0.4]; 
     [UIImageView commitAnimations]; 
     myMapView.zoomEnabled = NO; 
     myMapView.scrollEnabled = NO; 
    } else { 
     [UIImageView beginAnimations:nil context:nil]; 
     [UIImageView setAnimationDuration:0.5]; 
     [drawView setAlpha:0.0]; 
     [UIImageView commitAnimations]; 
     myMapView.zoomEnabled = YES; 
     myMapView.scrollEnabled = YES; 
    } 
} 

做我的画,后,我想这场平局变成一种形式(地区),这将被放在地图上。如果你对我有一些迹象?

谢谢你,对不起我的英语不好。

+0

保存此图作为一幅图像并将此图像用作地图视图上的注释。如果是工作:) – Mangesh

回答

0

这里是我的代码,如果任何人有同样的问题,我和艾伦给我的链接:http://www.apps168.net/post/1460.html

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event { 
    UITouch *touch = [touches anyObject]; 
    pointA = [touch locationInView:drawView]; 
    pathRef = CGPathCreateMutable(); 
    CGPathMoveToPoint(pathRef, NULL, pointA.x, pointA.y); 
} 

- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event { 
    UITouch *touch = [touches anyObject]; 
    CGPoint currentLocation = [touch locationInView:drawView]; 
    CGPoint pastLocation = [touch previousLocationInView:drawView]; 

    UIGraphicsBeginImageContext(drawView.frame.size); 
    CGContextRef ctx = UIGraphicsGetCurrentContext(); 
    [drawView.image drawInRect:CGRectMake(0, 0, drawView.frame.size.width, drawView.frame.size.height)]; 
    CGContextSetLineCap(ctx, kCGLineCapRound); 
    CGContextSetLineWidth(ctx, 5.0); 
    CGContextSetRGBStrokeColor(ctx, 0.0, 0.0, 0.0, 1.0); 
    CGContextBeginPath(ctx); 
    CGContextStrokePath(ctx); 

    CGContextMoveToPoint(ctx, pastLocation.x, pastLocation.y); 
    CGContextAddLineToPoint(ctx, currentLocation.x, currentLocation.y); 
    CGContextDrawPath(ctx, kCGPathFillStroke); 
    drawView.image=UIGraphicsGetImageFromCurrentImageContext(); 

    CGPathAddLineToPoint(pathRef, NULL, currentLocation.x, currentLocation.y); 
    UIGraphicsEndImageContext(); 

    //pointA = currentLocation; 
} 

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

    CGPathCloseSubpath(pathRef); 

    NSUInteger count = [array count]; 
    for (NSUInteger i = 0; i < count; i=i+2) { 
     NSString *lat = [array objectAtIndex: i]; 
     NSString *lgt = [array objectAtIndex: i+1]; 

     CLLocationCoordinate2D pointLoc; 
     pointLoc.latitude = [lat doubleValue]; 
     pointLoc.longitude = [lgt doubleValue]; 

     locationConverToImage = [myMapView convertCoordinate:pointLoc toPointToView:drawView]; 
     if (CGPathContainsPoint(pathRef, NULL, locationConverToImage, NO)) { 
      NSLog(@"point in path!"); 
      //sélection de ces points et cacher les autres. 
     } 
    } 

    [UIImageView beginAnimations:nil context:nil]; 
    [UIImageView setAnimationDuration:0.5]; 
    [drawView setAlpha:0.0]; 
    [UIImageView commitAnimations]; 
    myMapView.zoomEnabled = YES; 
    myMapView.scrollEnabled = YES; 

} 
相关问题