大头针显示多张图片

256 阅读1分钟

需求:一个大头针上要显示文字、图片1、图片2、图片3。

实现思路:设置一个大小适中的superView,把控件全部添加到view上,然后把view弄成image,设置给annotation.image即可。

#pragma mark - MAMapViewDelegate
- (MAAnnotationView *)mapView:(MAMapView *)mapView viewForAnnotation:(id<MAAnnotation>)annotation{
    if([annotation isKindOfClass:[MAPointAnnotation class]]){
        static NSString *reuseIdentifier = @"annotationReuseIndetifier";
        MAAnnotationView *annotationView = [mapView dequeueReusableAnnotationViewWithIdentifier:reuseIdentifier];
        if(annotationView == nil){
            annotationView = [[MAAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:reuseIdentifier];
        }
        annotationView.canShowCallout = NO;
       
        //存放控件的view。把view压成image
        UIView *baseView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 85, 82)];
        //设置偏移量
        carImageView.center = CGPointMake(baseView.centerX, baseView.centerY + 11);
        labelImageView.center = CGPointMake(baseView.centerX, baseView.centerY - 19);
        statusImageView.center = CGPointMake(baseView.centerX, baseView.centerY + 11);
        [baseView addSubview:carImageView];
        [baseView addSubview:labelImageView];
        [baseView addSubview:statusImageView];
        //大头针的大小是根据image的size来确定的。
        annotationView.image = [self bezierPathClip:baseView];
        return annotationView;
    }
    return nil;
}

//把view弄成图片
- (UIImage *)bezierPathClip:(UIView *)view{
    UIGraphicsBeginImageContextWithOptions(view.size, NO, [UIScreen mainScreen].scale);
    [view.layer renderInContext:UIGraphicsGetCurrentContext()];
    UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return image;
}