我试图在MKMapView中为MKPointAnnotation设置一个自定义图像。这些图像的设置是否正确适用于所有iPhone除了6+:MKAnnotation setImage适用于iPhone,但不适用于iPad/iPhone 6+
但在iPhone 6+和iPad,注释具有默认销,而不是我的图片:
以下是设置注释图像的代码,以及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 *图像打印在两种情况下,以下对象(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
}
}
这里是我的parking_lot_icon图像资产:
我可以看到的唯一区别是,iPad的使用了不同的图像规模;但我包括所有比例的图标。我觉得我失去了一些明显的东西。任何人都可以想到为什么我的图像可能会设置在iPhone而不是iPad/6 +的原因吗?
您添加了@ 3x图片吗? –
感谢Ashish,我确实在我的资产中添加了所有3个缩放图像。 http://i.imgur.com/DesHVXB.png –
你是否实现了'viewForAnnotation' _delegate_方法,并且你在那里创建了一个MKAnnotationView?直接调用viewForAnnotation并设置问题代码中显示的图像不是实现它的方式。 – Anna