从 MKMapView 中的 MKAnnotation 更改 UIImage
我有一个仅在 MKAnnotation 上的地图视图,它有一个服装图像。当用户更改地图类型时,我需要更改该注释的图像。
我这样做的方法是从地图中删除注释,然后插入另一个具有正确图像的注释,但我认为这不是最好的方法。显示新图像大约需要 1 或 2 秒。
如何在不删除注释并删除另一个注释的情况下做到这一点?
谢谢,
RL
I have a mapView with only on MKAnnotation, that has a costume image. When the user changes the mapType, I need to change the image of that annotation.
The way I did that was to remove the annotation from the map, and insert another with the correct image, bu i don't think is the best way. It takes about 1 ou 2 seconds to show the new image.
How can I do it without remove the annotation and drop another?
Thanks,
RL
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以使用地图视图的
viewForAnnotation:
实例方法(与其名称相似的委托方法不同)来获取注释的当前视图并显式强制图像更改。例如,在地图类型更改的地方:
但是,您也应该将完全相同的 if 语句添加到 viewForAnnotation 委托方法中,这样当地图视图稍后调用委托方法本身时,它就会也将设置正确的图像。
您可能希望将图像设置逻辑移至一个通用方法,您可以从更改地图类型的位置和
viewForAnnotation
委托方法(MKAnnotationView
对象将作为参数传递)。如果逻辑位于一处,则不必记住保持两处同步。You can use the
viewForAnnotation:
instance method of the map view (not the same as its delegate method with a similar name) to get the current view of the annotation and force the image change explicitly.For example, at the place where the map type is changed:
However, you should add the exact same if-statement to the
viewForAnnotation
delegate method also so when the map view later calls the delegate method itself, it will set the correct image also.You may want to move the image-setting logic to a common method that you can call from the place where you change the map type and from the
viewForAnnotation
delegate method (theMKAnnotationView
object would be passed as a parameter). If the logic is in one place, you don't have to remember to keep both places in sync.