MKAnnotationView 不显示图像

MKAnnotationView doesn't display image

这里有个小问题..

我已经完成了一些代码,这些代码应该可以更改在 mapView 中放置的图钉的图像。 "taxi.png" 的 url 适用于图像视图,因此这不是当前的问题。

我也试过:

pinView?.image = UIImage(named: "taxi.png")

代码:

 if pinView == nil{
        pinView = MKPinAnnotationView(annotation: annotation, reuseIdentifier: reuseId)
        pinView?.canShowCallout = true
        var imageTaxi = UIImage(named: "taxi.png")!
        imageTaxi = imageTaxi.withAlignmentRectInsets(UIEdgeInsetsMake(0, 0, imageTaxi.size.height/2, 0))
        pinView?.image = imageTaxi
        }

        let button = UIButton(type: .detailDisclosure)
        pinView?.rightCalloutAccessoryView = button
        return pinView

有没有人看到这里应该修复什么才能查看图像?

编辑: 我现在有了这个,我仍然有标准的红色针尖..

 let annotation = CustomPin()
 annotation.coordinate = location
 annotation.title = "Boek Taxi"
 annotation.subtitle = ""
 let image = UIImage(named: "taxi.png")

 annotation.Image = image
 self.mapView.addAnnotation(annotation)
 let reuseId = "pin"
    var pinView = mapView.dequeueReusableAnnotationView(withIdentifier: reuseId)

    if pinView == nil{
        pinView = MKPinAnnotationView(annotation: CustomPin() as MKAnnotation, reuseIdentifier: reuseId)
        pinView?.canShowCallout = true

    } else {
        pinView?.annotation = CustomPin() as MKAnnotation
    }

    let cpa = annotation as! CustomPin

    pinView?.image = cpa.Image

    let button = UIButton(type: .detailDisclosure)
    pinView?.rightCalloutAccessoryView = button
    return pinView
    }

有没有我看过的东西?

试试这个

为您的自定义注释创建 class 示例:

class CustomPin: MKPointAnnotation {

   let Image = UIImage

 }

然后在注释视图的函数中执行此操作

func mapView(mapView: MKMapView, viewForAnnotation annotation: MKAnnotation) -> MKAnnotationView? {
         let reuseId = "image"

        var pinView = mapView.dequeueReusableAnnotationViewWithIdentifier(reuseId)
        if pinView == nil {

            pinView = MKAnnotationView(annotation: customPin as MKAnnotation, reuseIdentifier: reuseId)
            pinView!.canShowCallout = true

        } else {

            pinView!.annotation = customPin as MKAnnotation
        }

        let cpa = annotation as! CustomPin
        pinView!.image = cpa.Image

        let button = UIButton(type: UIButtonType.detailDisclosure)

        pinView!.rightCalloutAccessoryView = button


        return anView


        }

当您在 viewDidLoad() 或您想要的任何内容中创建注释时,最后添加自定义图像:

                                let Annotation = customPin()
                                Annotation.coordinate = //cooridnate
                                Annotation.title = //title
                                Annotation.subtitle = //subtitle

                                let image = UIImage(named: "taxi.png")
                                Annotation.Image = image

希望对您有所帮助