自定义注释视图的图像不会出现在 MKMapView 中的设备构建上
我正在使用 iphone sdk 并尝试在 MKMapView 上为 AnnotationView 使用自定义图像。我的方法适用于我的应用程序中的另一个屏幕,但对于有问题的屏幕,我在模拟器中看到图像,但在设备上看不到图像。我基本上创建一个像这样的自定义视图:
@implementation CustomAnnotationView
- (id) initWithAnnotation:(id <MKAnnotation>)annotation reuseIdentifier:(NSString *)reuseIdentifier
{
if (self = [ super initWithAnnotation:annotation reuseIdentifier:reuseIdentifier ])
{
self.opaque = NO;
self.backgroundColor = [UIColor clearColor];
self.image = [UIImage imageNamed:@"MapIcon.png"];
}
return self;
}
@end
然后在 getViewForAnnotation 方法中我这样做:
// attempt to reuse
CustomAnnotationView* annotationView = (Custom AnnotationView*)[mapView dequeueReusableAnnotationViewWithIdentifier:@""];
// if reuse failed, create a new one
if (annotationView == nil)
{
annotationView = [[Custom AnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:@""];
}
return annotationView;
它在模拟器中工作,但在设备中不起作用。如果我停止使用自定义视图,它将在两者中放置红色图钉(即它有效)。有人见过这样的差异吗?
I am using the iphone sdk and trying to use a custom image for my AnnotationView on my MKMapView. My approach works fine for another screen in my app, but for the problematic one, I see the image in the simulator but not on the device. I basically create a custom view like this:
@implementation CustomAnnotationView
- (id) initWithAnnotation:(id <MKAnnotation>)annotation reuseIdentifier:(NSString *)reuseIdentifier
{
if (self = [ super initWithAnnotation:annotation reuseIdentifier:reuseIdentifier ])
{
self.opaque = NO;
self.backgroundColor = [UIColor clearColor];
self.image = [UIImage imageNamed:@"MapIcon.png"];
}
return self;
}
@end
Then in the getViewForAnnotation method I do this:
// attempt to reuse
CustomAnnotationView* annotationView = (Custom AnnotationView*)[mapView dequeueReusableAnnotationViewWithIdentifier:@""];
// if reuse failed, create a new one
if (annotationView == nil)
{
annotationView = [[Custom AnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:@""];
}
return annotationView;
It works in the simulator but never in the device. If I stop using a custom view it places the red pin in both (i.e. it works). Has anyone seen a discrepancy like this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
哇。我发现问题是 MapIcon.png 不存在。这解释了为什么它在设备上不起作用,但它没有解释我如何设法在模拟器中看到该图标。也许以某种扭曲的方式它找到了项目中的 MapIconLocale.png 文件。或者项目中可能有一些跟踪资源,因为我几周前曾经有一个 MapIcon.png 并删除了它(以某种方式删除了它)?
不管怎样,正确命名的图像显然可以解决这个问题。
Wow. I discovered that the problem was that MapIcon.png does not exist. That explains why it did not work on the device, but it doesn't explain how I managed to see the icon in the simulator. Perhaps in some twisted way it found the MapIconLocale.png file which is in the project. Or could there be some trace asset in the project because I used to have a MapIcon.png weeks ago and removed it (a copy somehow)?
Either way, having the correctly named image obviously fixed it.