我从网络服务器获取一些图片网址,我需要在mapview上显示。我为MKAnnotationView
添加了函数并解析了url,但它没有在地图视图中显示。以下是我的代码。如果我做错了,请告诉我。
- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>)annotation
{
static NSString *identifier = @"CustomAnnotation";
MKAnnotationView* annView = nil;
if ([annotation isKindOfClass:[MapViewAnnotation class]]) {
annView = (MKAnnotationView *) [mapView dequeueReusableAnnotationViewWithIdentifier:identifier];
if (annView == nil) {
annView = [[MKAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:identifier];
} else {
annView.annotation = annotation;
}
for(int i=0;i<array.count;i++) {
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
NSURL *iconurl = [NSURL URLWithString:[[array objectAtIndex:i]valueForKey:@"icon"]];
UIImage *image = [UIImage imageWithData:[NSData dataWithContentsOfURL:iconurl]];
dispatch_async(dispatch_get_main_queue(), ^ {
UIImageView *imageView = [[UIImageView alloc] initWithImage:image];
[annView addSubview:imageView];
[annView setFrame:CGRectMake(0, 0, imageView.frame.size.width, imageView.frame.size.height)];
});
});
}
UIButton*accessory = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
[accessory addTarget:self action:@selector(openDetail:) forControlEvents:UIControlEventTouchUpInside];
[accessory setFrame:CGRectMake(0, 0, 30, 30)];
[annView setRightCalloutAccessoryView:accessory];
}
[annView setEnabled:YES];
[annView setCanShowCallout:YES];
return annView;
}
答案 0 :(得分:1)
首先,请确保您已指定地图视图的委托,并确认您的viewForAnnotation
已被调用。
其次,您要将UIImageView
添加为MKAnnotationView
的子视图。通常,您只需设置image
本身的MKAnnotationView
属性。