MKPinAnnotationView下载的图像(@ 3x)不能用于6+

我在我的应用程序上使用地图,对于引脚删除,我想设置用户的图像,而不是默认引脚。

我正在下载用户图像,并按照我粘贴下面的代码进行设置。

对于不同规模的设备,我使用的图像名称按照设备规模,例如,

1对于非视网膜设备 – pin.png(尺寸30 x 30)

2对于视网膜设备 – pin@2x.png(尺寸60 x 60)

3对于6+设备 – pin@3x.png(尺寸90 x 90)

这里为1和2工作正常和图像负载完美但是对于6 +(3倍规模)它不工作

什么是问题是:

对于6+我下载PIN @ 3X图像,但在地图上它的大小是90×90,应该是30×30。因为它只是适用于从应用程序捆绑使用的图像。

对于pin@2x.png,它工作正常,并显示大小为30 x 30的2x图像。

我也试图通过设置图像的比例而不是解决scheme

MKPinAnnotationView:有三种以上的颜色可用吗?

我尽力解释实际问题,如果我错过了任何东西,或者是否需要设置任何东西,任何人都可以指导吗?

- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id<MKAnnotation>)annotation { MKPinAnnotationView *annotationView = nil; if ([annotation isKindOfClass:[MKUserLocation class]]) { return nil; } if ([annotation isKindOfClass:[KPAnnotation class]]) { //Custom annotation class for pin drop KPAnnotation *a = (KPAnnotation *)annotation; annotationView = (MKPinAnnotationView *)[mapView dequeueReusableAnnotationViewWithIdentifier:@"pin"]; if (annotationView == nil) { annotationView = [[MKPinAnnotationView alloc] initWithAnnotation:[a.annotations anyObject] reuseIdentifier:@"pin"]; } //Image View to add subview in MKPinAnnotationView UIImageView *imageView = [[UIImageView alloc] init]; UIImage * image = [UIImage imageNamed:@"pin.png"]; imageView.image=image; //Test URL - see image name here NSString *readAWSURL=@"<domainname.com>/pin@3x.png"; //Downloading image here to load with async way (SDWebImage) [imageView sd_setImageWithURL:[NSURL URLWithString:readAWSURL] placeholderImage:[UIImage imageNamed:@"pin.png"]]; annotationView.image=imageView.image; [annotationView addSubview:imageView]; annotationView.canShowCallout = YES; } return annotationView; } 

使用MKAnnotationView代替MKPinAnnotationView

 - (MKAnnotationView ) mapView: (MKMapView ) mapViewIn viewForAnnotation:(id<MKAnnotation>) annotation { static NSString *annotationViewReuseIdentifier = @"annotationViewReuseIdentifier"; MKAnnotationView annotationView = (MKAnnotationView )[mapView dequeueReusableAnnotationViewWithIdentifier:annotationViewReuseIdentifier]; if (annotationView == nil) { annotationView = [[MKAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:annotationViewReuseIdentifier]; } if ([annotation isKindOfClass:[MapPoint class]]) { annotationView.image = [UIImage imageNamed:@"orange.png"]; annotationView.canShowCallout = NO; } else{ annotationView.image = [UIImage imageNamed:@"blue.png"]; annotationView.canShowCallout = YES; } annotationView.annotation = annotation; annotationView.rightCalloutAccessoryView = [UIButton buttonWithType:UIButtonTypeDetailDisclosure]; return annotationView; } 

我现在的答案是为我工作。 它基于pod 'SDWebImage'pod 'SDWebImage' pod 'UIImage-Resize'和有胡须的答案@mokriya http://mokriya.tumblr.com/post/15342072745/dynamic-annotations-mkannotationview-for-ios只需添加类别MKAnnotationView(WebCache&#xFF09; :

 #import <MapKit/MapKit.h> @interface MKAnnotationView(WebCache) - (void)setImageWithUrl:(NSURL *)url; @end 

和实施(不是一个完美的,但工作):

 #import "MKAnnotationView+WebCache.h" #import "UIImageView+WebCache.h" #import "UIImage+Resize.h" #define kIconSize CGSizeMake(50, 50) @implementation MKAnnotationView(WebCache) - (void)setImageWithUrl:(NSURL *)url { [[[UIImageView alloc] init] sd_setImageWithURL:url completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, NSURL *imageURL) { self.image = [image resizedImageToSize:kIconSize]; }]; } @end 

并使用:

 #pragma mark - MKMapViewDelegate - (nullable MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>)annotation { static NSString *annotationViewReuseIdentifier = @"annotationViewReuseIdentifier"; MKAnnotationView *annotationView = (MKAnnotationView *)[mapView dequeueReusableAnnotationViewWithIdentifier:annotationViewReuseIdentifier]; if (!annotationView) { annotationView = [[MKAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:annotationViewReuseIdentifier]; annotationView.canShowCallout = YES; } annotationView.annotation = <YOUR ANNOTATION>; [annotationView setImageWithUrl:<HERE IS THE URL>]; return annotationView; }