我在我的应用程序上使用map,对于pin drop,我想设置用户的图像而不是默认图钉。
我正在下载用户图片并根据我粘贴的代码设置它。
对于不同规模的设备,我按照设备比例使用图像名称,例如,
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 x 90,应该是30 x 30.因为它只适用于我们使用应用程序包时的图像。
对于pin@2x.png,它工作正常并显示尺寸为30 x 30的2x图像。
我也通过设置图像比例来尝试以下解决方案
MKPinAnnotationView: Are there more than three colors available?
我已尽力解释实际问题,如果我遗漏任何事情或是否需要设置任何内容,请任何人指导吗?
代码
- (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;
}
答案 0 :(得分:0)
使用 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;
}
答案 1 :(得分:0)
我现在的答案对我而言。它基于pod 'SDWebImage'
,pod 'UIImage-Resize'
和来自@mokriya http://mokriya.tumblr.com/post/15342072745/dynamic-annotations-mkannotationview-for-ios的大胡子回答
只需添加类别MKAnnotationView(WebCache):
#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;
}