MKAnnotationView未在iOS中的mapview上显示图像
我从web服务器上获取一些图像url,需要在mapview上显示。我为MKAnnotationView未在iOS中的mapview上显示图像,ios,mkmapview,mkannotationview,Ios,Mkmapview,Mkannotationview,我从web服务器上获取一些图像url,需要在mapview上显示。我为MKAnnotationView添加了函数,并解析了url,但是它没有显示在地图视图中。下面是我的代码。如果我做错了什么,请告诉我 - (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>)annotation { static NSString *identifier = @"Custom
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;
}
-(MKAnnotationView*)地图视图:(MKMapView*)地图视图注释:(id)注释
{
静态NSString*标识符=@“CustomAnnotation”;
MKAnnotationView*annView=nil;
if([annotation isKindOfClass:[MapViewAnnotation class]]){
annView=(MKAnnotationView*)[mapView dequeueReusableAnnotationViewWithIdentifier:identifier];
如果(annView==nil){
annView=[[MKAnnotationView alloc]initWithAnnotation:annotation重用标识符:标识符];
}否则{
annView.annotation=注释;
}
对于(int i=0;i首先,确保已指定地图视图的委托,并确认您的viewForAnnotation
已被调用
第二,您正在添加UIImageView
作为MKAnnotationView
的子视图。通常,您只需设置MKAnnotationView
本身的image
属性。这段代码迭代了一系列图像URL,所有这些URL可能都重叠,这很奇怪。您为什么要这样做?如果您正在尝试的话虽然我不同意Matt的观点,即你不能异步更新注释视图(因为你可以),在实践中,我会和他一起去,因为这可能是不推荐的。如果你有一个标准的annotation view图像,请提前下载。我只是认为如果有人点击按钮,在检索图像的几秒钟内什么都没有发生,这将是一个令人沮丧的用户体验。