MKAnnotationView 图像不显示
我在 UIView 子类中有一个 MKMapView 实例,它通过实现 viewForAnnotation:(id
- (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 目录中。
任何有关为什么未加载的帮助将不胜感激!
I have an MKMapView instance in a UIView subclass which conforms to the MKMapViewDelegate protocol by implementing the viewForAnnotation:(id<MKAnnotation>) annotation
method. The code of which is:
- (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;
}
The image is not loading properly - the loaded image property has a height and width of zero and the dimensions of the MKAnnotationView instance a are also zero.
The image is 4Kb png 32 pixels x 32 pixels.
I can confirm that the image has been copied into the root .app directory in the simulator.
Any help as to why this isn't loading would be appreciated!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
为了这篇文章的完整性:您需要更改
MKAnnotationView* a = [[MKAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:@"driverView"];
到:
a = [[MKAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:@"driverView"];
For completeness for this post: You need to change
MKAnnotationView* a = [[MKAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:@"driverView"];
to:
a = [[MKAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:@"driverView"];
设置图像后尝试添加
[a sizeToFit]
。Try adding
[a sizeToFit]
after setting the image.我以前见过这个。对我来说解决这个问题的是重新导出我的 PNG。由于某种原因,其中一些会在其中获取元数据(例如 Fireworks 的东西),这会导致它在某些情况下出现错误。我见过这种情况主要发生在 Internet Explorer 上,但也见过 MKMapView 上发生的情况。更奇怪的是,它在模拟器中有效,但在设备上无效。
I have seen this before. What solved the problem for me was to re-export my PNGs. For some reason, some of them get metadata in them (like Fireworks stuff) which causes it to bug out in certain situations. I've seen that happen mostly with Internet Explorer, but have seen it with MKMapView as well. Even more strange, it worked in the simulator but not on the device.