通过MKMapView在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的button的代码:

编辑:我做了这个停止与地图的互动:

 - (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; } } 

画完之后, 我想把这个画画转换成一个表格 (区域),这个表格会放在地图上。 如果你有什么迹象表明我?

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

这里是我的代码,如果任何人有同样的问题,我和艾伦给我的链接: 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; } 

画我的地图试试这个,这是绘制地图点项目