第一次加载视图控制器时,我从 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;
}