Objective c iOS中的自定义批注图像

Objective c iOS中的自定义批注图像,objective-c,ios,annotations,mapkit,Objective C,Ios,Annotations,Mapkit,我正在尝试用特定的图像替换典型的iOS mapkit“pin”。我对编码还不熟悉,所以我不确定这到底是为什么不起作用,但以下是我尝试的: 对于方法 - (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>)annotation { 然而,即使代码编译得很好,pinView也没有被设置为pinImage。有什么不可以的吗?您只需将最后一行更改为: [pinView a

我正在尝试用特定的图像替换典型的iOS mapkit“pin”。我对编码还不熟悉,所以我不确定这到底是为什么不起作用,但以下是我尝试的:

对于方法

- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>)annotation
{

然而,即使代码编译得很好,pinView也没有被设置为pinImage。有什么不可以的吗?

您只需将最后一行更改为:

[pinView addSubview:pinImage];
完整示例:

- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>)annotation
{

    MKPinAnnotationView *annView = [[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:@"ParkingPin"];

    UIImageView *imageView = [[[UIImageView alloc] initWithImage:[UIImage imageNamed:@"pin_parking.png"]] autorelease];

    annView.animatesDrop = TRUE;
    annView.canShowCallout = YES;
    annView.calloutOffset = CGPointMake(-5, 5);
    [annView addSubview:imageView];
    return annView;
}
-(MKAnnotationView*)地图视图:(MKMapView*)地图视图注释:(id)注释
{
MKPinAnnotationView*annView=[[MKPinAnnotationView alloc]initWithAnnotation:annotation重用标识符:@“ParkingPin”];
UIImageView*imageView=[[UIImageView alloc]initWithImage:[UIImageName:@“pin_parking.png”]]自动释放];
annView.animatesDrop=TRUE;
annView.canShowCallout=是;
annView.calloutOffset=CGPointMake(-5,5);
[annView addSubview:imageView];
返回视图;
}

希望这有帮助

图像应设置为annView的按钮instand,我使用如下方式:

UIButton *button = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
[button setImage:[UIUtils getStyleImage:@"page_map_accessory_button.png"] forState:UIControlStateNormal];
[button setImage:[UIUtils getStyleImage:@"page_map_accessory_button.png"] forState:UIControlStateSelected];
[button setImage:[UIUtils getStyleImage:@"page_map_accessory_button.png"] forState:UIControlStateHighlighted];
pinView.rightCalloutAccessoryView = button;
只要做:

annView.image=[UIImage imageNamed:@"imagename.png"];

我在这里没有看到的答案是,您必须在viewForAnnotation方法中退出队列或创建一个MKAnnotationView,并且将您的类作为地图视图的委托,以便它调用此方法来获取视图


如果您在其他地方创建了注释视图,则使用image属性设置的任何自定义图像都不会显示,您只会看到一个pin。

只需从代码中删除“animatesDrop”行即可。它会对您有用。

请参阅、等。据我所知,PinAnnotationView应该只使用内置的pin图像,因此我认为它不支持设置自定义图像。。。
annView.image=[UIImage imageNamed:@"imagename.png"];