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