2016-12-02 24 views
0

PhotoAnnotation符合MKAnnotation,PhotoAnnotationView是MKAnnotationView的子類。 PhotoAnnotation具有圖像屬性。無法立即顯示自定義AnnotationView的圖像

問題是一旦我打開應用程序,自定義註釋的圖像不會顯示在mapview上。我必須平移並移動到某個地方,然後移回原始區域,然後圖像纔會顯示出來。使用MKPinAnnotation的

沒有這樣的問題。銷的圖像將立即顯示。

我不知道如何解決它。請幫幫我。非常感謝。

func mapView(_ mapView: MKMapView, viewFor annotation: MKAnnotation) -> MKAnnotationView? { 
    if let annotation = annotation as? PhotoAnnotation { 
     let identifier = "pin" 
     let view: PhotoAnnotationView 
     if let dequeuedView = mapView.dequeueReusableAnnotationView(withIdentifier: identifier) as? PhotoAnnotationView { 
      dequeuedView.annotation = annotation 

      dequeuedView.image = annotation.image 

      view = dequeuedView 
     } else { 
      view = PhotoAnnotationView(annotation: annotation, reuseIdentifier: identifier) 
      view.canShowCallout = false 
      view.image = nil 
     } 
     return view 
    } 
    return nil 
} 

回答

0

要注意這個部分,「view.image =零」的......因爲在那裏沒有標註之前還創建了第一個負載,它屬於你設置你的形象,零代碼。處理,你很好。

func mapView(_ mapView: MKMapView, viewFor annotation: MKAnnotation) -> MKAnnotationView? { 
    if let annotation = annotation as? PhotoAnnotation { 
     let identifier = "pin" 
     let view: PhotoAnnotationView 
     if let dequeuedView = mapView.dequeueReusableAnnotationView(withIdentifier: identifier) as? PhotoAnnotationView { 
      dequeuedView.annotation = annotation 

      dequeuedView.image = annotation.image 

      view = dequeuedView 
     } else { 
      view = PhotoAnnotationView(annotation: annotation, reuseIdentifier: identifier) 
      view.canShowCallout = false 
      view.image = UIImage() // do something here... 
     } 
     return view 
    } 
    return nil 
} 
+0

仍然不起作用。 –

+0

我添加其他圖片,現在是工作。謝謝 –

+0

沒問題。別客氣。只要將問題標記爲已回答的問題。 – nferocious76