MKPinAnnotationView下载的图像(@ 3x)不适用于6+ [英] MKPinAnnotationView downloaded image (@3x) not working for 6+

查看:150
本文介绍了MKPinAnnotationView下载的图像(@ 3x)不适用于6+的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用我的应用程序中的地图,对于引脚丢弃,我想设置用户的图像,而不是默认的引脚。

I am using map on my application, For pin drop, I want to set user's image instead of default pin.

我正在下载用户的图像并将其设置为

I am downloading users image and setting it as per code I paste below.

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

For different scale of devices I am using image name as per device scale like eg.,

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

2对于视网膜设备 - 针@ 2x.png(大小60 x 60)

3对于6+以上设备 - pin@3x.png(大小90 x 90)

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

Here for 1 and 2 working fine and image loads perfect But for 6+ (3x scale) it's not working

问题是:

对于6+我正在下载pin @ 3x图像,但在地图上它的大小是90 x 90应该是30 x 30.因为它只适用于从应用程序包使用的图像。

For 6+ I am downloading pin@3x image, But on map it's size is 90 x 90 that should be 30 x 30. As it simply works for images when we use from application bundle.

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

For pin@2x.png it works fine and shows 2x image in size 30 x 30.

我还通过设置图片的比例来尝试下面的解决方案。

I also tried below solution by setting scale of image but not works

MKPinAnnotationView:是否有三种颜色可供选择?

我尽力解释实际问题,如果我没有任何东西或者是否需要设置任何内容?

I have tried my best to explain actual issue, Can any one guide please if I am missing any thing or if it's require to set anything?

代码

- (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;

}


推荐答案

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

My answer working nowadays for me. It based on pod 'SDWebImage',pod 'UIImage-Resize' and bearded answer from @mokriya http://mokriya.tumblr.com/post/15342072745/dynamic-annotations-mkannotationview-for-ios Just add category MKAnnotationView(WebCache):

#import <MapKit/MapKit.h>

@interface MKAnnotationView(WebCache)

- (void)setImageWithUrl:(NSURL *)url;

@end

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

and implementation (not a perfect but worked one):

#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;
}

这篇关于MKPinAnnotationView下载的图像(@ 3x)不适用于6+的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆