更改地图注释视图的图像原点?

常规注释引脚的原点位于底部中间,因此引脚始终指向同一位置。

但是当我添加自定义图像时,它的原点是图像的中心,所以每次放大或缩小,我的图像底部指向不同的位置。

在此处输入图像描述

在这里我的别针应该指向巴黎的中心但是

在此处输入图像描述

但是当我放大时,我的针脚底部并没有指向巴黎的中心。

我正在尝试使用CGRect.origin但没有得到任何有用的东西。

这是我的代码:

 - (MKAnnotationView *)mapView:(MKMapView *)theMapView viewForAnnotation:(id )annotation { MKAnnotationView * customPinView = [[MKAnnotationView alloc] init]; UIImage * img = [UIImage imageNamed:@"waterPin.png"] ; CGRect resizeRect; resizeRect.size.height = 40; resizeRect.size.width = 40; resizeRect.origin = (CGPoint){0.0f, 0.0f}; UIGraphicsBeginImageContext(resizeRect.size); [img drawInRect:resizeRect]; UIImage *resizedImage = UIGraphicsGetImageFromCurrentImageContext(); UIGraphicsEndImageContext(); customPinView.image = resizedImage; return customPinView; } 

MKAnnotationView具有centerOffset属性,您可以尝试设置以调整图像偏移:

 customPinView.centerOffset = CGPointMake(xOffset,yOffset); 

不相关,但您应该使用initWithAnnotation而不是init来创建MKAnnotationView
使用dequeueReusableAnnotationViewWithIdentifier并实现注释视图重用以提高性能也没有什么坏处。

我还建议不要以编程方式调整委托方法中的图像大小,而是使用已经resize的图像开始。 然后你可以做customPinView.image = [UIImage imageNamed:@"resizedWaterPin.png"]; 无需花费运行时间每次调整注释图像的大小。