2
我从Web服务器获取一些图像URL,我需要在mapview上显示该图像。我添加了MKAnnotationView
的功能并解析了网址,但是它没有在地图视图中显示。以下是我的代码。请让我知道,如果我做错了什么。MKAnnotationView在iOS的mapview上未显示图像
- (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;
}
这段代码遍历图像URL数组,这些代码都可能重叠,这很奇怪。你为什么这样做?如果您尝试制作动画,则不是这样做的方法。 – Rob
虽然我不同意马特的观点,你不能异步地更新注释视图(因为你可以),但实际上,我可能会不同意他的看法。如果您有标准注释视图图像,请提前下载。我认为如果有人点击按钮,在检索图像时几秒钟内没有任何事情发生,这将是一个令人沮丧的用户体验。 – Rob