2016-09-30 29 views
2

這裏有一個小問題..MKAnnotationView不顯示圖像

我已經做了一些代碼,應該改變得到的在圖形頁面放棄了銷的形象。 「taxi.png」的網址適用於圖片視圖,因此這不是當前的問題。

我也試過:

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 
    } 

任何事情我已經漏掉了?

+0

看一看這個[自定義圖釘圖像(http://stackoverflow.com/a/38159048/4080925) – MwcsMac

+0

確認該代碼獲取調用所有(設置斷點和/或打印日誌語句)。也許代表沒有被指定,或者方法簽名是不正確的。如果不是這樣,他們請澄清一下:當你說有一個「問題」時,你究竟看到了什麼?紅色針?沒有?崩潰? – Rob

回答

-1

試試這個

創建一個類爲您定製標註 例子:

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 

希望這有助於

+0

@Rob在我的應用程序中正常工作,完全符合他的要求 –

+0

感謝您的答案@TripPhillips,但是當我這樣做時,我仍然有紅色標準的精確位置。您可以查看我上傳的代碼問題帖子。我會非常感謝 – royvano

+0

@royvano您需要使用MKAnnotationView而不是MKAnnotationPinView。這是你的問題。所以在你定義pinView的地方使用MKAnnotationView。如果你使用MKAnnotationPinView,你每次都會得到一個pin。 –