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

Mic*_*ian 0 objective-c mapkit ios

到目前为止,我只有默认的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;
}
Run Code Online (Sandbox Code Playgroud)

Iva*_*and 5

MKPinAnnotationView是它的子类MKAnnotationView,它具有image属性但忽略它,而是显示默认的pin图像.

你应该使用MKAnnotationView相反的.

此外,请确保您的MKMapView代表没有被占用.