iphone - MKAnnotationView 图像不显示

标签 iphone ios mkmapview mkannotationview

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

- (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 目录中。

如果您能提供有关为何未加载的任何帮助,我们将不胜感激!

最佳答案

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

到:

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

关于iphone - MKAnnotationView 图像不显示,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9439987/

相关文章:

ios - 如何将大量数据加载到 CoreData 中

swift - 如何在 map 下方的固定大小组件中显示 MKPinAnnotationView 数据(例如标题、副标题)而不是图钉上方的弹出窗口?

objective-c - 由于 Xcode 8 中有太多未处理的消息而丢弃事件 0 的消息

iPhone 应用程序正常工作,启动时突然崩溃,重新安装修复程序,但为什么?

iphone - 如何解决 iOS 应用程序中的核心位置逻辑

ios - 出现此错误时如何退出facebook sdk登录?

ios - ReactiveCocoa - SignalProducer,发出数组中最新的 N 个值

swift - 如何在移动 map 时更改注释的大小?

iPhone - 将标签栏与多个导航栏相结合

iphone - 直接从 Xcode 编译/应用程序