如何将UIImageView添加到MKOverlayRenderer中?

3

由于MKOverlayView不再支持视图,而是使用MKOverlayRenderer,那么我如何向我的MKMapView中添加UIImageView呢?请注意:我知道如何通过在CGContextRef上绘制它来将图像添加到地图上,但我特别需要添加UIImageView。谢谢!


我正在尝试做同样的事情。你找到答案了吗? - wigging
2个回答

2
这里是如何添加UIImage。如果您想添加UIImageView,那么苹果文档中指出(参见Apple Doc MKOverlayRenderer):
推荐使用Core Graphics来绘制叠加层的任何内容。如果您选择使用UIKit类和方法进行绘制,则必须将指定的图形上下文推送到上下文堆栈中(使用UIGraphicsPushContext函数),然后再进行任何绘制调用。完成绘图后,您必须同样使用UIGraphicsPopContext从堆栈中弹出图形上下文。通常在后台线程上绘制叠加层以提高性能,因此不要操作UIKit视图或其他只能在应用程序的主线程上操作的对象。
#import <Foundation/Foundation.h>

@import MapKit;

@interface MyOverlayRenderer : MKOverlayRenderer

@end

实现方式如下:
#import "MyOverlayRenderer.h"

@implementation MyOverlayRenderer

- (void)drawMapRect:(MKMapRect)mapRect zoomScale:(MKZoomScale)zoomScale inContext:(CGContextRef)context {
    UIImage *image = [UIImage imageNamed:@"indigo_eiffel_blog.png"];
    CGImageRef imageReference = image.CGImage;

    MKMapRect theMapRect = [self.overlay boundingMapRect];
    CGRect theRect = [self rectForMapRect:theMapRect];

    CGContextScaleCTM(context, 1.0, -1.0);
    CGContextTranslateCTM(context, 0.0, -theRect.size.height);

    CGContextDrawImage(context, theRect, imageReference);
}

@end

-1

如果您正在使用视图,则应该向MKAnnotationView添加子视图。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接