2016-09-18 7 views
2

WebViewから表示する必要がある画像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

私はマットの主張に同意しませんが、実際にはアノテーションビューを非同期的に更新することはできませんが、実際には推奨されないことがあります。標準注釈ビュー画像をお持ちの場合は、事前にダウンロードしてください。誰かがボタンをタップしてイメージが取得されている間に何も起こらない場合、私はそれが不満なUXになるだろうと思う。 – Rob

答えて

1

まず、あなたはマップビューのデリゲートを指定して、あなたのviewForAnnotationがすべてで呼び出さなっていることを確認していることを確認します。

第2に、UIImageViewMKAnnotationViewのサブビューとして追加しています。通常はMKAnnotationView自体のimageプロパティを設定するだけです。

関連する問題