1

I have this code into the - (void)drawMapRect:(MKMapRect)mapRect zoomScale:(MKZoomScale)zoomScale inContext:(CGContextRef)context method (into a MKOverlayView subclass) to prevent drawing segments that are less than 10 pixels long on a map overlay :

            CGPoint origin = [self pointForMapPoint:poly.points[0]];
            CGPoint lastPoint = origin;

            CGContextMoveToPoint(context, origin.x, origin.y);

            for (int i=1; i<poly.pointCount; i++) {
                CGPoint point = [self pointForMapPoint:poly.points[i]];

                CGFloat xDist = (point.x - lastPoint.x);
                CGFloat yDist = (point.y - lastPoint.y);
                CGFloat distance = sqrt((xDist * xDist) + (yDist * yDist)) * zoomScale;

                if (distance >= 10.0) {
                    lastPoint = point;
                    CGContextAddLineToPoint(context, point.x, point.y);
                }
            }

will the test >= 10.0 will take care about the screen resolution, or may I introduce some [UIScreen mainScreen].scale parameter ?

Oliver
  • 23,072
  • 33
  • 138
  • 230

2 Answers2

3

I believe that test >= 10.0 does not take into account the screen resolution. Apple does most of their drawing arithmetic using "points" instead of pixels- that way code does not have to change for a retina display compared to a normal display.

If you want to draw something just 10.0 pixels wide, you will need to take into account the screen resolution; however, if you do this you'll have to write the method to support both retina display and normal display.

Carter
  • 3,053
  • 1
  • 17
  • 22
  • It's in the `- (void)drawMapRect:(MKMapRect)mapRect zoomScale:(MKZoomScale)zoomScale inContext:(CGContextRef)context` method (MKOverlayView subclass). What do you think I should do ? – Oliver Jul 21 '11 at 09:06
  • The test will ignore the screen resolution, so you need to ask if you're trying not to draw something 10 pixels wide or something 10 points wide. I would go with points (because it transitions between devices better) and then you don't have to change your current code. – Carter Jul 21 '11 at 17:20
  • I don't want to ignore it, I want line in pixels size, not point. should I insert .scale property ? – Oliver Jul 21 '11 at 22:33
  • 1
    Yes, then I think you should put in the .scale property. Definitely test it out to make sure that it is working, but I think that will work. – Carter Jul 21 '11 at 22:36
2

It depends on how the graphics context is configured. If this is in UIView drawing code, the view's scale factor (which is set automatically) will take care of this, if you're drawing into a bitmap context, you have to do it manually.

omz
  • 53,243
  • 5
  • 129
  • 141
  • It's in the `- (void)drawMapRect:(MKMapRect)mapRect zoomScale:(MKZoomScale)zoomScale inContext:(CGContextRef)context` method (MKOverlayView subclass). May I do something like >= 10.0 / self.view.scale ? – Oliver Jul 21 '11 at 09:08
  • There's nothing else mentioned in the docs, so I'd assume that the scale factor is already set in the context. Btw, this method runs on a background thread, so you have to be a bit careful. – omz Jul 21 '11 at 14:53
  • being careful about what ? What is the relation between the scale and the fact it's a background thread ? – Oliver Jul 21 '11 at 15:47