1

第一次加载视图控制器时,我从 Web 服务下载数据,然后使用详细信息添加注释。当我点击按钮再次下载数据并重新输入时,每次都是错误的照片。我试图清除缓存并没有帮助,我尝试了很多方法。有任何想法吗?

编辑:

- (void)addCustomersToMap
{
    marker = nil;
    profilePics = [[NSMutableArray alloc] init];

    if ([self.usersNearMe isKindOfClass:[NSDictionary class]])
    {
        NSDictionary *singleUser = (NSDictionary*)self.usersNearMe;
        self.usersNearMe = [[NSArray alloc] initWithObjects:singleUser, nil];
    }
    if ([self.usersNearMe isKindOfClass:[NSArray class]])
    {
        for (int  i = 0; i < self.usersNearMe.count; i++)
        {
            NSDictionary *dict = self.usersNearMe[i];
            float lat = [[dict objectForKey:@"Lat"] floatValue];
            float lon = [[dict objectForKey:@"Lon"] floatValue];
            NSString *name = [dict objectForKey:@"Name"];
            NSString *time = [dict objectForKey:@"Time"];
            NSString *picUrl = [dict objectForKey:@"PictureUrl"];
            [profilePics addObject:picUrl];
            CLLocationCoordinate2D mLoc = CLLocationCoordinate2DMake(lat, lon);


            if ([picUrl isEqualToString:@"empty"])
            {
                marker = [[CustomerMarker alloc] initWithLocation:mLoc title:name andSubtitle:time andUrlString:nil andImagePerson:[UIImage imageNamed:@"user"]];
            }
            else if ([picUrl isEqualToString:@"system"])
            {
                marker = [[CustomerMarker alloc] initWithLocation:mLoc title:name andSubtitle:time andUrlString:nil andImagePerson:[UIImage imageNamed:@"police_icon"]];
            }
            else
            {
                NSString *completeURL = @"";
                completeURL = [completeURL stringByAppendingString:kPROFILE_IMAGE_URL];
                completeURL = [completeURL stringByAppendingString:@"/2/"];
                completeURL = [completeURL stringByAppendingString:picUrl];
                completeURL = [completeURL stringByAppendingString:@".png"];


                NSURL *url = [NSURL URLWithString:completeURL];
                NSURLRequest *urlRequest = [NSURLRequest requestWithURL:url];
                [NSURLConnection sendAsynchronousRequest:urlRequest queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse *response, NSData *responseData, NSError *error) {
                    if (responseData) {
                        NSString  *imageName = completeURL.lastPathComponent;
                        NSString *imagePath = [[self documentsDirectoryPath] stringByAppendingString:imageName];
                        NSLog(@"imagePath: %@", imagePath);
                        [responseData writeToFile:imagePath atomically:YES];
                        marker = [[CustomerMarker alloc] initWithLocation:mLoc title:name andSubtitle:time andUrlString:nil andImagePerson:[UIImage imageWithContentsOfFile:imagePath]];

                        dispatch_async(dispatch_get_main_queue(), ^{
                            [self.btnRefreshUsers setImage:[UIImage imageWithContentsOfFile:imagePath] forState:UIControlStateNormal];
                            [self.mapView viewForAnnotation:marker];
                            [self.mapView addAnnotation:marker];
                        });
                    }
                }];
            }
        }
    }

    self.btnRefreshUsers.userInteractionEnabled = YES;
}



- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id<MKAnnotation>)annotation
{
    dispatch_async(dispatch_get_main_queue(), ^{
        if ([annotation isKindOfClass:[MKUserLocation class]])
        {
            [self.mapView selectAnnotation:((MKUserLocation*) annotation) animated:YES];
        }
    });

    if ([annotation isKindOfClass:[MKUserLocation class]])
    {
        ((MKUserLocation*) annotation).title = NSLocalizedString(@"current_location", nil);
        return nil;
    }

    if ([annotation isKindOfClass:[CustomerMarker class]])
    {
        MKAnnotationView *pinView = nil;
        static NSString *customerMarkerPinID = @"com.idm.customer";
        pinView = (MKAnnotationView*)[mapView dequeueReusableAnnotationViewWithIdentifier:customerMarkerPinID];

        if (!pinView)
        {
            pinView = [[MKAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:customerMarkerPinID];
        }

        pinView.canShowCallout = YES;
        pinView.backgroundColor = [UIColor clearColor];


        CustomerMarker *customerMarker = (CustomerMarker*)annotation;
        pinView.image = customerMarker.imagePerson;

        CGRect pinViewFrame = CGRectZero;
        pinViewFrame.origin.x = pinView.frame.origin.x;
        pinViewFrame.origin.y = pinView.frame.origin.y;
        pinViewFrame.size.width = 40.0f;
        pinViewFrame.size.height = 40.0f;
        pinView.frame = pinViewFrame;


        ////////// Animate Pin Drop //////////

        CGRect endFrame = pinView.frame;
        pinView.frame = CGRectOffset(pinView.frame, 0, -230);

        [UIView beginAnimations:nil context:nil];
        [UIView setAnimationDuration:0.45f];
        [UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];

        pinView.frame = endFrame;

        [UIView commitAnimations];

        /* CGRect endFrame = pinView.frame;
        pinView.frame = CGRectOffset(pinView.frame, 0, -230);
        [UIView animateWithDuration:0.45f delay:0 options:UIViewAnimationOptionShowHideTransitionViews animations:^{
            [UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];
        } completion:^(BOOL finished) {
            if (finished) {
                pinView.frame = endFrame;
            }
        }]; */

        return pinView;
    }

    return nil;
}
4

1 回答 1

2

这里有两个主要问题。首先,您正在重用 pinView,但没有根据它尝试绘制的注释设置属性。将可重用的annotationView 出列是正确的,但是如果您重用一个或不重用一个,您将始终需要将图像设置为您现在要绘制的图像,否则pinView 的图像将是以前的图像。例如

if ([annotation isKindOfClass:[CustomerMarker class]])
{
    MKAnnotationView *pinView = nil;//Create a new handle, don't risk having one global one
    static NSString *customerMarkerPinID = @"com.idm.customer";
    pinView = (MKAnnotationView*)[mapView dequeueReusableAnnotationViewWithIdentifier:customerMarkerPinID];

    if (!pinView)
    {
        pinView = [[MKAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:customerMarkerPinID];

        //Since all your pinViews can show callouts you only need to set this
        //when creating a new one, not when reusing an existing one which will
        // already have it set
        pinView.canShowCallout = YES;
    }
        pinView.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"map_pic"]];

    ...   

    return pinView;
}

//[self.mapView setNeedsDisplay];  <--- Don't do this
return nil;

其次,您根据profilePicsCounter添加图像时更改的每个图钉选择图像,但mapView 可以并且会以任何顺序请求视图,而不是您添加它们的顺序。viewForAnnotation所以你需要根据它给你的注释计算视图来响应。线索就在名字里。这一行:

NSString *urlString = profilePics[profilePicsCounter];

需要像

MyAnnotationClass *myAnno = (MyAnnotationClass)annotation
NSString *urlString = myAnno.urlString;

本质上,您必须将所需的信息存储在注释中。

此外,函数的注释部分removeAllMarkers可以缩短为一行

[self.mapView removeAnnotations:self.mapView.annotations];

你在你的 pinViews 上设置了 backgroundColor 两次

于 2013-09-03T21:04:11.197 回答