How to display an image on a MKOverlayView?

前端 未结 6 1136
故里飘歌
故里飘歌 2020-12-05 00:59

UPDATE:

Images who are projected on the MKMapView using a MKOverlayView use the Mercator projection, while the image that I use as input data uses

6条回答
  •  温柔的废话
    2020-12-05 01:38

    I'm not sure if this would affect the scaling issue, but in your OverlayView code, your drawing the entire image for every maptile.

    Have you tried only drawing the portion of the image that is visible in mapRect?

    When I've had problems with MKOverlayViews, its been helpful for me to draw the rect of the overlay (self.overlay.boundingRect) and the mapRect (passed into drawMapRect). Although, I'm not sure if drawing the mapRect would be helpful in your situation.

    At any rate, heres the function I use to draw the rect in case you want to try it out

    -(void)drawRect:(MKMapRect)rect inContext:(CGContextRef)context withLineWidth:(float)lineWidth andColor:(CGColorRef)color
    {
    
        double maxx = MKMapRectGetMaxX(rect);
        double minx = MKMapRectGetMinX(rect);
        double maxy = MKMapRectGetMaxY(rect);
        double miny = MKMapRectGetMinY(rect);
    
        CGPoint tr = [self pointForMapPoint:(MKMapPoint) {maxx, maxy}];
        CGPoint br = [self pointForMapPoint:(MKMapPoint) {maxx, miny}];
        CGPoint bl = [self pointForMapPoint:(MKMapPoint) {minx, miny}];
        CGPoint tl = [self pointForMapPoint:(MKMapPoint) {minx, maxy}];
    
        CGMutablePathRef cgPath = CGPathCreateMutable();
        CGPathMoveToPoint(cgPath, NULL, tr.x, tr.y);
        CGPathAddLineToPoint(cgPath, NULL, br.x, br.y);
        CGPathAddLineToPoint(cgPath, NULL, bl.x, bl.y);
        CGPathAddLineToPoint(cgPath, NULL, tl.x, tl.y);
        CGPathAddLineToPoint(cgPath, NULL, tr.x, tr.y); 
    
        CGContextSaveGState(context);
        CGContextAddPath(context, cgPath);
        CGContextSetStrokeColorWithColor(context, color);
        CGContextSetLineJoin(context, kCGLineJoinRound);
        CGContextSetLineCap(context, kCGLineCapRound);
        CGContextSetLineWidth(context, lineWidth);
        CGContextStrokePath(context);
        CGPathRelease(cgPath);  
        CGContextRestoreGState(context);
    }
    

提交回复
热议问题