不会显示自定义 Pin 图片

Won't Show Custom Pin Image

我正在尝试让自定义图钉图像显示在我的 mapView 中,它显示的是常规红色图钉而不是我的自定义图像。我做错了什么?

查看注释:

func mapView(_ mapView: MKMapView, viewFor annotation: MKAnnotation) -> MKAnnotationView? {
        if annotation is MKUserLocation {
            return nil
        }
        let reuseID = "pin"
        
        var pinView = mapView.dequeueReusableAnnotationView(withIdentifier: reuseID) as? MKPinAnnotationView
        if(pinView == nil) {
            pinView = MKPinAnnotationView(annotation: annotation, reuseIdentifier: reuseID)
            pinView!.canShowCallout = true
            pinView!.animatesDrop = false
            pinView?.image = UIImage(named: "CustomPinImage")
            pinView!.rightCalloutAccessoryView = UIButton(type: UIButtonType.detailDisclosure) as UIButton
            let smallSquare = CGSize(width: 40, height: 40)
            let button = UIButton(frame: CGRect(origin: CGPoint.zero, size: smallSquare))
            button.setBackgroundImage(UIImage(named: "Car2"), for: UIControlState())
            pinView?.leftCalloutAccessoryView = button
            
        }
        else
        {
            pinView?.annotation = annotation
        }
        
        return pinView
        
    }

我如何显示 Pin 图:

let LitzmanLocation = CLLocationCoordinate2DMake(32.100668,34.775192)
        let Litzman = MKPointAnnotation()
        Litzman.coordinate = LitzmanLocation
        Litzman.title = "Litzman Bar"
        Litzman.subtitle = "נמל תל אביב 18,תל אביב"
        mapView.addAnnotation(Litzman)

如果有人能帮我解决这个问题就太好了!

谢谢:)

试试这个代码:

 func mapView(_ mapView: MKMapView, viewFor annotation: MKAnnotation) -> MKAnnotationView? {

        if annotation.isMember(of: MKUserLocation.self) {
            return nil
        }

        let reuseId = "pin"

        var pinView = mapView.dequeueReusableAnnotationView(withIdentifier: reuseId)
        if pinView == nil {
            pinView = MKAnnotationView(annotation: annotation, reuseIdentifier: reuseId)}
        pinView!.canShowCallout = true
        pinView!.image = UIImage(named: "CustomPinImage")
        pinView?.rightCalloutAccessoryView = UIButton(type: UIButtonType.detailDisclosure) as UIButton
        let smallSquare = CGSize(width: 40, height: 40)
        let button = UIButton(frame: CGRect(origin: CGPoint.zero, size: smallSquare))
        button.setBackgroundImage(UIImage(named: "Car2"), for: UIControlState())
        pinView?.leftCalloutAccessoryView = button

        return pinView

    }