【发布时间】:2015-05-02 13:23:27
【问题描述】:
我正在尝试在我的 MKMapView 中为 MKPointAnnotation 设置自定义图像。除了 6+ 以外,所有 iPhone 上的图像都设置正确:
但在 iPhone 6+ 和 iPad 上,注释有默认 pin 而不是我的图片:
下面是设置注解图片的代码,以及Images.xcassets中parking_spot_icon.png的入口。我的代码都不依赖于设备的大小,并且没有相关的错误或构建警告(特别是没有关于不正确图像大小的警告)。
MKPointAnnotation *annotation = [[MKPointAnnotation alloc] init];
annotation.coordinate = parkingLot->coordinate;
[_mapView addAnnotation:annotation];
[_mapView selectAnnotation:annotation animated:YES]; // this is needed for the image to be set correctly on iphone.
[_mapView deselectAnnotation:annotation animated:YES]; // this is needed for the image to be set correctly on iphone.
annotation.title = parkingLot->name;
UIImage *image = [UIImage imageNamed:@"parking_spot_icon.png"];
[[_mapView viewForAnnotation:annotation] setImage:image];
我尝试单步执行代码,我看到 UIImage *image 在两种情况下(iPhone 和 iPad)都打印为以下对象:
(lldb) p *image
(UIImage) $0 = {
NSObject = {
isa = UIImage
}
_imageRef = 0x00007fd2d733fd30 // differs run-to-run
_scale = 3 // 2 on iPhone, 3 on iPad
_imageFlags = {
named = 1
imageOrientation = 0
cached = 0
hasPattern = 0
isCIImage = 0
renderingMode = 0
suppressesAccessibilityHairlineThickening = 0
hasDecompressionInfo = 0
}
}
这是我的 park_lot_icon 图像资源:
我能看到的唯一区别是 iPad 使用了不同的图像比例;但我包括了所有尺度的图标。我觉得我错过了一些明显的东西。谁能想到为什么我的图像可以设置在 iPhone 上而不是 iPad/6+ 上?
【问题讨论】:
-
你添加了@3x 图片吗?
-
感谢 Ashish,我确实在我的资产中添加了所有 3 个比例图像。 i.imgur.com/DesHVXB.png
-
你是否实现了
viewForAnnotationdelegate 方法并且你在那里创建了一个MKAnnotationView?直接调用viewForAnnotation并按照问题中的代码设置图像不是这样做的方法。 -
感谢 Anna,将我的 setImage 放入 viewForAnnotation 工作!现在我在所有设备上都有自定义注释。
标签: ios ipad mkmapview mkannotation mkannotationview