iPhone - 以像素和屏幕分辨率为单位的 CGPoint 距离
我将此代码放入 - (void)drawMapRect:(MKMapRect)mapRect ZoomScale:(MKZoomScale)zoomScale inContext:(CGContextRef)context
方法(放入 MKOverlayView 子类)中,以防止绘制小于地图覆盖上 10 像素长:
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);
}
}
测试 >= 10.0 会考虑屏幕分辨率吗,或者我可以介绍一些 [UIScreen mainScreen].scale
参数?
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 ?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我相信 test >= 10.0 没有考虑屏幕分辨率。苹果公司的大部分绘图算法都使用“点”而不是像素——这样,与普通显示器相比,视网膜显示器的代码就不必改变。
如果您想绘制宽度仅为 10.0 像素的内容,则需要考虑屏幕分辨率;但是,如果您这样做,您将必须编写方法来支持视网膜显示和正常显示。
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.
这取决于图形上下文的配置方式。如果这是在
UIView
绘图代码中,则视图的比例因子(自动设置)将处理此问题,如果您正在绘制位图上下文,则必须手动执行此操作。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.