2015-05-02 131 views
1

我试图在MKMapView中为MKPointAnnotation设置一个自定义图像。这些图像的设置是否正确适用于所有iPhone除了6+:MKAnnotation setImage适用于iPhone,但不适用于iPad/iPhone 6+

enter image description here

但在iPhone 6+和iPad,注释具有默认销,而不是我的图片:

enter image description here

以下是设置注释图像的代码,以及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图像资产:

enter image description here

我可以看到的唯一区别是,iPad的使用了不同的图像规模;但我包括所有比例的图标。我觉得我失去了一些明显的东西。任何人都可以想到为什么我的图像可能会设置在iPhone而不是iPad/6 +的原因吗?

+0

您添加了@ 3x图片吗? –

+1

感谢Ashish,我确实在我的资产中添加了所有3个缩放图像。 http://i.imgur.com/DesHVXB.png –

+0

你是否实现了'viewForAnnotation' _delegate_方法,并且你在那里创建了一个MKAnnotationView?直接调用viewForAnnotation并设置问题代码中显示的图像不是实现它的方式。 – Anna

回答

1

要设置注释视图的自定义图像,您必须实现viewForAnnotation委托方法方法。

您不能调用地图视图的viewForAnnotation实例方法,并设置image对这一观点,因为这将是一个一次性执行,不会“粘”到视图。

当地图视图稍后需要此视图或其他注释的视图(这可能会在注释添加后很长时间),它会调用委托方法(如果已实施)。如果代理方法未实现,则地图视图会为您显示默认的红色针脚。

它在iPhone上“运行”而不是在iPad上的事实只是随机的,不可预知的行为,你不应该依赖它。

样品实施viewForAnnotation委托方法:

-(MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id<MKAnnotation>)annotation 
{ 
    if ([annotation isKindOfClass:[MKUserLocation class]]) { 
     //if this is the user location, return nil so map view 
     //draws default blue dot for it... 
     return nil; 
    } 

    static NSString *reuseId = @"ann"; 

    MKAnnotationView *av = [mapView dequeueReusableAnnotationViewWithIdentifier:reuseId]; 
    if (av == nil) { 
     av = [[MKAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:reuseId]; 
     av.canShowCallout = YES; 
     UIImage *image = [UIImage imageNamed:@"parking_spot_icon.png"]; 
     av.image = image; 
    } 
    else { 
     //we are recycling a view previously used for another annotation, 
     //update its annotation reference to the current one... 
     av.annotation = annotation; 
    } 

    return av; 
} 

确保地图视图的delegate属性设置或使得其出口连接到故事板视图控制器/ XIB否则委托方法不会得到即使它已经实施了,也被称为。


创建注释时,您还应该删除对 selectAnnotationdeselectAnnotation的调用 - 它们不是必需的。

+0

非常感谢您的更新,它也回答了我的下一个问题,即“如何更改除用户位置以外的所有注释?” –

+0

HI,M得到相同的问题,它适用于视网膜设备上的(@ 2x)图像,但对于6+我需要(@ 3x)图像,并且它不适用于6+。请帮忙吗? – Niks

+0

可以检查,如果你知道这样的问题。 http://stackoverflow.com/questions/30476231/mkpinannotationview-downloaded-image-3x-not-working-for-6 – Niks

相关问题