2016-08-02 4 views
3

내 주석 이미지를 맞춤형 이미지로 변경하는 데있어 다른 스택 게시물과 자습서를 따라 왔지만 제대로 작동하지 않는 것 같습니다. 오류 또는 런타임 오류가 나타나지 않으며 주석 이미지가 변경되지 않습니다.Swift - 주석 이미지 변경 중

방금 ​​내가 줄 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")) 
} 

답변

4

문제 확인하는 경우 중단 점에 확인이라고 당신 ' MKPinAnnotationView을 다시 사용하십시오.

class ViewController: UIViewController, MKMapViewDelegate { 

    @IBOutlet weak var mapView: MKMapView! 

    private func addAnnotation(coordinate coordinate: CLLocationCoordinate2D, title: String, subtitle: String) { 
     let annotation = MKPointAnnotation() 
     annotation.coordinate = coordinate 
     annotation.title = title 
     annotation.subtitle = subtitle 
     mapView.addAnnotation(annotation) 
    } 

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

     let identifier = "CustomAnnotation" 

     var annotationView = mapView.dequeueReusableAnnotationViewWithIdentifier(identifier) 

     if annotationView == nil { 
      annotationView = MKAnnotationView(annotation: annotation, reuseIdentifier: identifier) 
      annotationView!.canShowCallout = true 
      annotationView!.image = UIImage(named: "star.png")! // go ahead and use forced unwrapping and you'll be notified if it can't be found; alternatively, use `guard` statement to accomplish the same thing and show a custom error message 
     } else { 
      annotationView!.annotation = annotation 
     } 

     return annotationView 
    } 

    ... 
} 

가 항복 : 당신이 MKAnnotationView를 사용하는 경우, 당신은 당신의 이미지를 볼 수

Chicago star

과거 (예를 들어 아이폰 OS 8)에서, MKPinAnnotationViewimage가 잘 작동하는 것 같았다 설정 만에 iOS 9.x, 핀을 상관없이 사용하는 것 같습니다. MKPinAnnotationView 클래스의 경우 완전히 비합리적인 동작이 아니며 MKAnnotationView을 사용하면이 문제를 피할 수 있습니다.

+0

정말 고마워요! –

0

전화 수익

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

return annotationView 

전에하고 너무 프로젝트에 우리에게 이미지의 스크린 샷을 보여 내부의 경우 :