【问题标题】:MKAnnotationView image not displayingMKAnnotationView 图像不显示
【发布时间】:2012-02-25 00:28:58
【问题描述】:

我在 UIView 子类中有一个 MKMapView 实例,它通过实现viewForAnnotation:(id<MKAnnotation>) annotation 方法符合 MKMapViewDelegate 协议。其中的代码是:

- (MKAnnotationView*) mapView:(MKMapView *)mapView viewForAnnotation:(id<MKAnnotation>)annotation {
if ([annotation isKindOfClass:[DriverLocation class]]) {
    MKAnnotationView* a = [self.map dequeueReusableAnnotationViewWithIdentifier:@"driverView"];
    if (a == nil) {
        MKAnnotationView* a = [[MKAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:@"driverView"];
        a.enabled = YES;
        a.image = [UIImage imageNamed:@"car.png"];
    }
    return a;
}
return nil;

}

图像未正确加载 - 加载的图像属性的高度和宽度为零,并且 MKAnnotationView 实例 a 的尺寸也为零。

图片为 4Kb png 32 像素 x 32 像素。

我可以确认图像已经被复制到模拟器的根 .app 目录中。

任何关于为什么不加载的帮助将不胜感激!

【问题讨论】:

  • 对不起。我是个白痴。我创建了两个名为 a 的变量,if 语句中 a 的实例化应该只是 a = ... 而不是 MKAnnotationView* a。

标签: iphone ios mkmapview mkannotationview


【解决方案1】:

为了这篇文章的完整性:您需要更改 MKAnnotationView* a = [[MKAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:@"driverView"];

到:

a = [[MKAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:@"driverView"];

【讨论】:

    【解决方案2】:

    设置图片后尝试添加[a sizeToFit]

    【讨论】:

      【解决方案3】:

      我以前见过这个。为我解决问题的是重新导出我的 PNG。出于某种原因,其中一些会在其中获取元数据(例如 Fireworks 的东西),这会导致它在某些情况下出错。我已经看到这种情况主要发生在 Internet Explorer 上,但在 MKMapView 上也看到过。更奇怪的是,它在模拟器上工作,但在设备上却不行。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多