Draw on UIImageView over MKMapView

时光总嘲笑我的痴心妄想 提交于 2019-12-13 06:01:43

问题


I have searched a lot an answer for this question, but none of them seem to do exactly what I want. A lot of tutorials show me how to add lines and polygons in code, but not with freehand drawing.

I got a MKMapView with a UIImageView over the map. And I can draw a little on my UIImageView but right after that, the MKMapView is draged and the draw doesn't continue.

I would like to know what have I done wrong ?

Here is my code for the touches events :

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

And the code of the button who show the UIImageView :

EDIT : I made this for stoping the interactions with the map :

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

After doing my draw, I would like to transform this draw into a form (area), that will be put on the map. If you have some indications for me ?

Thank you, and sorry for my bad english.


回答1:


Here is my code if anyone have the same problem as me and the link that Allan gave me : 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;

}



回答2:


Draw my map try this , this is draw map point project



来源:https://stackoverflow.com/questions/16083980/draw-on-uiimageview-over-mkmapview

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!