如何通过 Objective-C 添加自定义图像以替换 iOS MKMapView 中的默认图钉?

How to add custom image to replace default pin in iOS MKMapView via Objective-C?

所以到目前为止,我只有默认的 MKMapView 引脚在工作。但我想要一个自定义图像来替换那个红色的小别针。这就是我下面的内容。我错过了什么?我认为定义 view.image 可以解决问题,但我想不会。

- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:   
  (id<MKAnnotation>)annotation {
    MKPinAnnotationView *view = [[MKPinAnnotationView alloc]  
    initWithAnnotation:annotation reuseIdentifier:@"pin"];
    view.enabled = YES;
    view.animatesDrop = YES;
    view.canShowCallout = YES;
    view.image = [UIImage imageNamed:@"custom-pin-image.png"];

    UIImageView *imageView = [[UIImageView alloc] initWithImage:[UIImage         
     imageNamed:@"custom-image-for-annotation.png"]];
    view.leftCalloutAccessoryView = imageView;
    view.rightCalloutAccessoryView = [UIButton buttonWithType:  
     UIButtonTypeDetailDisclosure];

    return view;
}

MKPinAnnotationViewMKAnnotationView 的子类,它有图像 属性 但它会忽略它并显示默认的图钉图像。

您应该改用 MKAnnotationView

此外,请确保您的 MKMapView 委托没有被取消。