【问题标题】:MKAnnotationView not showing image on mapview in iOSMKAnnotationView 未在 iOS 的 mapview 上显示图像
【发布时间】:2016-09-18 03:59:27
【问题描述】:

我正在从网络服务器获取一些图像 url,我需要将其显示在 mapview 上。我为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;
}

【问题讨论】:

  • 这段代码遍历了一组图像 URL,可能都是重叠的,非常奇怪。你为什么这样做?如果您尝试制作动画,这不是这样做的方法。
  • 虽然我不同意 Matt 的论点,即您不能 异步更新注释视图(因为您可以),但实际上,我会同意他,因为这可能不推荐.如果您有标准的注释视图图像,请提前下载。我只是认为如果有人点击按钮并且在检索图像时几秒钟内没有任何反应,这将是一个令人沮丧的用户体验。

标签: ios mkmapview mkannotationview


【解决方案1】:

首先,确保您已指定地图视图的委托,并确认您的 viewForAnnotation 已被调用。

其次,您要将UIImageView 添加为MKAnnotationView 的子视图。通常您只需设置MKAnnotationView 本身的image 属性即可。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-06-12
    • 1970-01-01
    • 1970-01-01
    • 2021-11-19
    • 1970-01-01
    • 2021-12-11
    • 1970-01-01
    • 2014-11-13
    相关资源
    最近更新 更多