百度地图 大头针设置本地图片与网络图片

百度地图 大头针设置,一般情况下会加载本地图片,本地图片会有@2x @3x 两张图,百度地图会根据图片的大小来设置大头针的图片大小,机型适配之后系统会自动选择对应的@2x @3x 图片,可是如果大头针(部分大头针图片确定,很小部分不确定),需要从后天获取,后台上传一张图片(只上传一张)比如 100px * 100px,如果直接拿后台的这张图片设置在大头针上则在5 和 6p 上会有不同的效果,5 上很大 6p 上很小,如何解决这种适配问题呢?

1. 首先想到是将大头针的尺寸设置固定不变,再把图片填充上去,百度的设置图片的 

BMKPinAnnotationView 直接设置图片,不能设置其内部图片的frame,要么就只能自定义写个大头针控件,但并不想这么做


2.后台给了一张图片,是否可在这张图片上进行操作呢?我的做法是将后台拿到的图片 根据分辨率 重绘图片之后设置图片。


- (BMKAnnotationView *)mapView:(BMKMapView *)mapView viewForAnnotation:(id <BMKAnnotation>)annotation

{

    if ([annotation isKindOfClass:[ClusterAnnotation class]]) {// 聚合状态

        NSString *AnnotationViewID = @"ClusterMark";

        ClusterAnnotation *cluster = (ClusterAnnotation*)annotation;

        ClusterAnnotationView *cluAnnotationView = [[ClusterAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:AnnotationViewID];

        // 添加聚合类的点击手势

        UITapGestureRecognizer* cluTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(cluViewClick:)];

        [cluAnnotationView addGestureRecognizer:cluTap];

        cluAnnotationView.size = cluster.size;

        cluAnnotationView.annotation = cluster;

        

        return cluAnnotationView;

    }else if ([annotation isKindOfClass:[LocalModel class]]) { 

       YLBMKPinAnnotationView *newAnnotationView = [[YLBMKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:@"myAnnotation"];

        // 普通状态下的大头针添加点击手势

        newAnnotationView.userInteractionEnabled = YES;

        UITapGestureRecognizer* tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(animationViewDidClick:)];

        [newAnnotationView addGestureRecognizer:tap];

        

        LocalModel *tempModel = (LocalModel *)annotation;

        newAnnotationView.paopaoView.hidden = YES;

        newAnnotationView.animatesDrop = NO;

        

        if ((tempModel.pic_app_url.length > 0) && tempModel.showImageType != kStationShowImageTypeYellow) { //网络图片

            UIImage* localImage = self.sever_stationImages[tempModel.pic_app_url];

            if (localImage) { // 已下载过

                newAnnotationView.image = [UIImage scaleToSizeImg:localImage scalRation:[UIScreen mainScreen].scale];

                

            }else{ // 未下载

                   // 设置占位图

                    newAnnotationView.image = [UIImage imageNamed:@"station_gray.png"];

                    // 下载图片

                    [self downLoadStationGroupImageWith:tempModel annotation:newAnnotationView];

            }

        }else{ // 本地图片

            if (tempModel.type == kLocationTypeSelf) {

                UIImage *image = [UIImage imageNamed:@"my_car.png"];

                newAnnotationView.image = image;

            }

            else if (tempModel.showImageType == kStationShowImageTypeBlue) {

                UIImage *image = [UIImage imageNamed:@"station_blue.png"];

                newAnnotationView.image = image;

            }

            else if (tempModel.showImageType == kStationShowImageTypeYellow) {

                //UIImage *image = [UIImage imageNamed:@"StationImageYellow.png"];

                UIImage *image = [UIImage imageNamed:@""];

                

                newAnnotationView.image = image;

            }

            else if (tempModel.showImageType == kStationShowImageTypeGray) {

                UIImage *image = [UIImage imageNamed:@"station_gray.png"];

                newAnnotationView.image = image;

            }

        }

        return newAnnotationView;

    }

    BMKAnnotationView* norAnotaion = [[BMKAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:@"nor"];

    norAnotaion.image = [UIImage imageNamed:@"StationImageYellow.png"];

    return norAnotaion;

}


// 下载图片

-( void )downLoadStationGroupImageWith:( LocalModel *)tempModel annotation:( YLBMKPinAnnotationView *)newAnnotationView

{

    dispatch_async(dispatch_get_global_queue(0, 0), ^{

        NSData* imageData = [NSData dataWithContentsOfURL:[NSURL URLWithString:tempModel.pic_app_url]];

        if(imageData == nilreturn ;

        UIImage *image = [UIImage imageWithData:imageData];

        // 回到主线程更新 UI

        dispatch_async(dispatch_get_main_queue(), ^{

            newAnnotationView.image = [UIImage scaleToSizeImg:image scalRation:[UIScreen mainScreen].scale];

            // 将图片写进文件

//            NSData* data = UIImagePNGRepresentation(image);

//            [data writeToFile:kFileImagePath(tempModel.pic_app_url) atomically:YES];

        });

        self.sever_stationImages[tempModel.pic_app_url] = image;

        // 将照片文件存储进沙盒 获取 cashs 沙盒文件路径

    });

}


/*

 *   UIimage 分类  根据下载图片重绘图片

 */

#import "UIImage+Scal.h"


@implementation UIImage (Scal)

+(UIImage *)scaleToSizeImg:(UIImage *)img scalRation:(CGFloat)scal

{

    CGSize size = CGSizeZero;

    CGFloat scale_screen = [UIScreen mainScreen].scale;

    if(scale_screen < 3){

        size = CGSizeMake(100, 100);

    }else{

        size = CGSizeMake(150150));

    }

    UIGraphicsBeginImageContext(size);

    // 绘制改变大小的图片

    [img drawInRect:CGRectMake(0,0, size.width, size.height)];

    // 从当前context中创建一个改变大小后的图片

    UIImage* scaledImage =UIGraphicsGetImageFromCurrentImageContext();

    // 使当前的context出堆栈

    UIGraphicsEndImageContext();

    //返回新的改变大小后的图片

    return scaledImage;

}


此种方式后台只需要传一张图片,也可以实现机型适配





  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值