0

我正在向 MKMapView 添加一些注释,这些注释有一个 UIImageView,与 AFNetworking 异步加载,作为左标注附件视图。这是实现:

- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id<MKAnnotation>)annotation
{
    static NSString *identifier = @"MapAnnotation";

    if ([annotation isKindOfClass:[MapAnnotation class]]) {

        MKAnnotationView *annotationView = (MKAnnotationView *)[_mapView dequeueReusableAnnotationViewWithIdentifier:identifier];
        if (annotationView == nil) {
            annotationView = [[MKAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:identifier];
            annotationView.enabled = YES;
            annotationView.canShowCallout = YES;
            annotationView.image = [UIImage imageNamed:@"map_pin"];

            UIImageView *userAvatar = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 30, 30)];
            [userAvatar setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"https://graph.facebook.com/%@/picture?type=small",[[(MapAnnotation *)annotation user] facebookID]]] placeholderImage:[UIImage imageNamed:@"user_placeholder"]];
            userAvatar.layer.cornerRadius = 4.0;
            userAvatar.layer.masksToBounds = YES;
            userAvatar.layer.borderColor = [[UIColor blackColor] CGColor];
            userAvatar.layer.borderWidth = 1;

            annotationView.leftCalloutAccessoryView = userAvatar;

            UIButton *rightCallout = [UIButton buttonWithType:UIButtonTypeInfoLight];

            annotationView.rightCalloutAccessoryView = rightCallout;

        }

        annotationView.annotation = annotation;

        return annotationView;
    }

    return nil;
}

这里的问题是:有时(并非所有时候)将不同用户的注释加载到地图中时,它们会为所有用户显示相同的图像。

假设我必须使用此信息加载两个注释:

注释 1 姓名(标题):Joshua 日期时间(副标题):19/06, 11:00 图片:www.myurl.com/joshua.jpg

注释 2 名称(标题):Mathew 日期时间(副标题):10/06, 20:00 图片:www.myurl.com/mathew.jpg

有时它们正确显示了标题和副标题,但加载的图像对于所有这些都是相同的(约书亚的图像,反之亦然)。

我在这里遗漏了什么是这种方法吗?

4

1 回答 1

1

调用dequeueReusableAnnotationViewWithIdentifier:将在第一次调用返回nil时满足annotationView == nil并设置您的注释视图。因此,所有以下调用都dequeueReusableAnnotationViewWithIdentifier:将返回第一个创建的带有分配的头像图像的注释视图。

所以[userAvatar setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"https://graph.facebook.com/%@/picture?type=small",[[(MapAnnotation *)annotation user] facebookID]]] placeholderImage:[UIImage imageNamed:@"user_placeholder"]];只调用一次。

您需要将注释特定的更改移动到“if nil”设置范围之外的注释视图。

因此,您应该在每次 mapview 请求 annotationView 时设置 userAvatar 图像。所以是这样的:

MKAnnotationView *annotationView = (MKAnnotationView *)[_mapView dequeueReusableAnnotationViewWithIdentifier:identifier];
        if (annotationView == nil) {
            annotationView = [[MKAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:identifier];
            annotationView.enabled = YES;
            annotationView.canShowCallout = YES;
            annotationView.image = [UIImage imageNamed:@"map_pin"];

            UIImageView *userAvatar = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 30, 30)];
            userAvatar.layer.cornerRadius = 4.0;
            userAvatar.layer.masksToBounds = YES;
            userAvatar.layer.borderColor = [[UIColor blackColor] CGColor];
            userAvatar.layer.borderWidth = 1;

            annotationView.leftCalloutAccessoryView = userAvatar;

            UIButton *rightCallout = [UIButton buttonWithType:UIButtonTypeInfoLight];

            annotationView.rightCalloutAccessoryView = rightCallout;
        }
[((UIImageView *)annotationView.leftCalloutAccessoryView) setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"https://graph.facebook.com/%@/picture?type=small",[[(MapAnnotation *)annotation user] facebookID]]] placeholderImage:[UIImage imageNamed:@"user_placeholder"]];

这将在每次地图视图请求注释视图时分配正确的图像。

干杯莫顿

于 2013-06-19T20:23:37.107 回答