从MKMapView中的MKAnnotation更改UIImage [英] change UIImage from MKAnnotation in the MKMapView

查看:119
本文介绍了从MKMapView中的MKAnnotation更改UIImage的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个只有MKAnnotation的mapView,它有一个服装图像。当用户更改mapType时,我需要更改该注释的图像。

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.

我这样做的方法是从地图中删除注释,然后插入另一个注释正确的形象,我不认为是最好的方式。显示新图像大约需要1 2秒。

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?

谢谢,

RL

推荐答案

你可以使用地图视图的 viewForAnnotation:实例方法(与具有相似名称的委托方法不同)来获取注释的当前视图并强制更改图像显式。

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:

MKAnnotationView *av = [mapView viewForAnnotation:annotation];

if (mapView.mapType == MKMapTypeHybrid)
    av.image = [UIImage imageNamed: @"hybrid.png"];
else
    av.image = [UIImage imageNamed: @"standard.png"];

但是,您应该将完全相同的if语句添加到 viewForAnnotation 委托方法也是如此,当地图视图稍后调用委托方法本身时,它也将设置正确的图像。

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.

您可能希望将图像设置逻辑移动到可以从更改地图类型的位置和 viewForAnnotation 委托方法( MKAnnotationView 对象将作为参数传递)。如果逻辑在一个地方,你不必记住保持两个地方同步。

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 (the MKAnnotationView 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.

这篇关于从MKMapView中的MKAnnotation更改UIImage的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆