Swift - 更改注释图像不起作用

Swift - Changing Annotation Image Not Working

我已按照其他堆栈帖子和教程将我的注释图像更改为自定义图像,但它似乎不起作用。没有出现错误或运行时错误,只是注释图像没有改变。

顺便说一句,我在annotationView!.image = UIImage(named: "RaceCarMan2png.png")行设置了一个断点,它显示正在调用该行,但没有任何反应。我将衷心感谢您的帮助。谢谢

func mapView(mapView: MKMapView, viewForAnnotation annotation: MKAnnotation) -> MKAnnotationView? {
    if annotation is MKUserLocation {
        return nil
    }

    let identifier = "MyCustomAnnotation"

    var annotationView = mapView.dequeueReusableAnnotationViewWithIdentifier(identifier)
    if annotationView == nil {
        annotationView = MKPinAnnotationView(annotation: annotation, reuseIdentifier: identifier)
        annotationView?.canShowCallout = true

        annotationView!.image = UIImage(named: "RaceCarMan2png.png")

    } else {
        annotationView!.annotation = annotation
    }


    configureDetailView(annotationView!)

    return annotationView
}

func configureDetailView(annotationView: MKAnnotationView) {
        annotationView.detailCalloutAccessoryView = UIImageView(image: UIImage(named: "url.jpg"))
}

在 return

之前调用
if let annotationView = annotationView {
    // Configure your annotation view here
    annotationView.image = UIImage(named: "RaceCarMan2png.png")
}

return annotationView

然后用断点检查里面是否正常if

问题是您正在使用 MKPinAnnotationView。如果您使用 MKAnnotationView,您将看到您的图像。

在过去(例如iOS 8),设置MKPinAnnotationViewimage似乎工作正常,但在iOS 9及以后,它使用别针,不管怎样(这对于一个叫做 MKPinAnnotationView 的 class 来说并不是完全不合理的行为;lol)。

使用 MKAnnotationView 可以避免这个问题。