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

Ios MKPinAnnotationView下载的图像(@3x)不适用于6+;,ios,objective-c,dictionary,mkannotation,mkpinannotationview,Ios,Objective C,Dictionary,Mkannotation,Mkpinannotationview,我在我的应用程序中使用map,对于pindrop,我想设置用户的图像而不是默认pin 我正在下载用户图片,并按照下面粘贴的代码进行设置 对于不同规模的设备,我使用图像名称,如 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

我在我的应用程序中使用map,对于pindrop,我想设置用户的图像而不是默认pin

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

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

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的2倍图像

我也尝试了下面的解决方案,通过设置图像的比例,但不起作用

我已经尽了最大的努力来解释实际问题,如果我遗漏了什么或者需要设置什么,可以请任何一个向导吗

代码

- (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*)地图视图:(MKMapView*)地图视图注释:(id)注释
{
MKPinAnnotationView*annotationView=nil;
if([annotation isKindOfClass:[MKUserLocation类]])
{
返回零;
}
if([annotation isKindOfClass:[KPAnnotation class]])
{
//针掉落的自定义注释类
KPAnnotation*a=(KPAnnotation*)注释;
annotationView=(MKPinAnnotationView*)[mapView出列重用annotationView,标识符:@“pin”];
如果(注释视图==nil)
{
annotationView=[[MKPinAnnotationView alloc]initWithAnnotation:[a.annotations anyObject]
重用标识符:@“pin”];
}
//要在MKPinAnnotationView中添加子视图的图像视图
UIImageView*imageView=[[UIImageView alloc]init];
UIImage*image=[UIImage ImageName:@“pin.png”];
image=image;
//测试URL-请参见此处的图像名称
NSString*readAWSURL=@”/pin@3x.png";
//在此处下载图像以异步方式加载(SDWebImage)
[imageView sd_setImageWithURL:[NSURL URLWithString:readAWSURL]占位符图像:[UIImage ImageName:@“pin.png”];
annotationView.image=imageView.image;
[注释视图添加子视图:图像视图];
annotationView.canShowCallout=是;
}
返回注释视图;
}

使用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;
}
-(MKAnnotationView)映射视图:(MKMapView)视图中的映射视图用于注释:(id)注释
{
静态NSString*annotationViewReuseIdentifier=@“annotationViewReuseIdentifier”;
MKAnnotationView annotationView=(MKAnnotationView)[mapView出列可重用AnnotationViewWithIdentifier:annotationViewReuseIdentifier];
如果(注释视图==nil)
{
annotationView=[[MKAnnotationView alloc]initWithAnnotation:annotation reuseIdentifier:annotationViewReuseIdentifier];
}
if([annotation isKindOfClass:[MapPoint class]]){
annotationView.image=[UIImage ImageName:@“orange.png”];
annotationView.canShowCallout=否;
}
否则{
annotationView.image=[UIImage ImageName:@“blue.png”];
annotationView.canShowCallout=是;
}
annotationView.annotation=注释;
annotationView.rightCalloutAccessoryView=[UIButton Button,类型:UIButtonTypedTailDisclosure];
返回注释视图;
}

我的答案现在对我有用。它基于
pod'SDWebImage'
pod'UIImage Resize'
和@mokriya的胡子回答 只需添加类别MKAnnotationView(网络缓存):

并使用:

#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;
}
#pragma标记-MKMapViewDelegate
-(可为空的MKAnnotationView*)地图视图:(MKMapView*)地图视图用于注释:(id)注释{
静态NSString*annotationViewReuseIdentifier=@“annotationViewReuseIdentifier”;
MKAnnotationView*annotationView=(MKAnnotationView*)[mapView dequeueReusableAnnotationViewWithIdentifier:annotationViewReuseIdentifier];
如果(!annotationView){
annotationView=[[MKAnnotationView alloc]initWithAnnotation:annotation reuseIdentifier:annotationViewReuseIdentifier];
annotationView.canShowCallout=是;
}
annotationView.annotation=;
[annotationView setImageWithUrl:];
返回注释视图;
}

嗯,我遇到的问题是下载图片并在地图上显示。捆绑包中的显示图像没有问题。从bundle中显示它工作正常。
#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;
}