2016-09-18 8 views
2

mapview에 표시해야하는 웹 서버에서 이미지 URL을 얻고 있습니다. 내가 MKAnnotationView에 대한 기능을 추가하고지도보기에 표시되지 않는 URL을 구문 분석했습니다. 아래는 제 코드입니다. 내가 잘못한 일이 있으면 알려줘.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; 
} 
+0

이미지 URL 배열을 반복하는이 코드는 모두 겹칠 가능성이 매우 높습니다. 왜 그것을하고 있습니까? 애니메이션을 만들려고한다면이 방법이 아닙니다. – Rob

+0

주석의 관점을 비동기 적으로 업데이트 할 수 없다는 Matt의 주장에 동의하지 않지만 (실제로는 가능하기 때문에) 실제로는 권장하지 않는 한 그와 함께 할 것입니다. 표준 주석보기 이미지가있는 경우 미리 다운로드하십시오. 나는 누군가가 버튼을 두드리고 이미지가 검색되는 동안 몇 초 동안 아무 일도 일어나지 않는다면 그것은 실망스런 UX가 될 것이라고 생각한다. – Rob

답변

1

먼저,지도보기의 대리자를 지정하고 viewForAnnotation 전혀이라는지고 있는지 확인했는지 확인.

두 번째로 UIImageViewMKAnnotationView의 하위보기로 추가합니다. 일반적으로 MKAnnotationView 자체의 image 속성을 설정하기 만하면됩니다.