1

我正在制作一个使用 MKMapView 的应用程序。我添加了自定义引脚(带图像)。现在,当我放大然后缩小时,引脚会变回默认值(红色)。

这是我的代码:

    - (MKAnnotationView *) mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>) annotation
    {
        static NSString* SFAnnotationIdentifier = @"Kamera";
        MKPinAnnotationView* pinView =
        (MKPinAnnotationView *)[mapView dequeueReusableAnnotationViewWithIdentifier:SFAnnotationIdentifier];
        if (!pinView)
        {
            MKAnnotationView *annotationView = [[MKAnnotationView alloc] initWithAnnotation:annotation
                                                                             reuseIdentifier:SFAnnotationIdentifier];
            annotationView.canShowCallout = NO;

            UIImage *flagImage = [UIImage imageNamed:@"pinModer.png"];

            CGRect resizeRect;

            resizeRect.size = flagImage.size;
            resizeRect.size = CGSizeMake(40, 60);
            resizeRect.origin = (CGPoint){0.0f, 0.0f};
            UIGraphicsBeginImageContext(resizeRect.size);
            [flagImage drawInRect:resizeRect];
            UIImage *resizedImage = UIGraphicsGetImageFromCurrentImageContext();
            UIGraphicsEndImageContext();
            annotationView.image = resizedImage;
            annotationView.opaque = NO;

            UIImageView *sfIconView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"kameraNaprejModra.png"]];
            annotationView.leftCalloutAccessoryView = sfIconView;

            return annotationView;

    }    
    return nil;
}
4

1 回答 1

1

该代码没有处理dequeue返回非零的情况pinView(意味着它正在重新使用以前的注释视图)。

如果pinView不是nil,该方法将在返回nil注释视图的最后一行结束。

当您返回nil时,地图视图会绘制默认的注释视图,即红色大头针。


像这样调整代码:

if (!pinView)
{
    //no changes to code inside this if
    //...
    return annotationView;
}
//add an else part and return pinView instead of nil...
else
{
    pinView.annotation = annotation;
}

return pinView;
于 2012-11-02T15:52:22.613 回答