地图上添加自定义的大头针
大头针的图片是网络图片,之前整的都特卡,搜了半天,解决方法如下,加载网络图片就不卡了
- (BMKAnnotationView *)mapView:(BMKMapView *)mapView viewForAnnotation:(id )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 == nil) return ;
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(150,150));
}
UIGraphicsBeginImageContext(size);
// 绘制改变大小的图片
[img drawInRect:CGRectMake(0,0, size.width, size.height)];
// 从当前context中创建一个改变大小后的图片
UIImage* scaledImage =UIGraphicsGetImageFromCurrentImageContext();
// 使当前的context出堆栈
UIGraphicsEndImageContext();
//返回新的改变大小后的图片
return scaledImage;
}
参考文章: https://blog.csdn.net/lele9096_bk/article/details/68942794