简体   繁体   English

如何将UIImageView添加到MKOverlayRenderer?

[英]How to add UIImageView to MKOverlayRenderer?

Now since they're no more views in MKOverlayView, and it's called MKOverlayRenderer, how could I add an UIImageView to my MKMapView? 既然他们在MKOverlayView中没有更多的视图,并且它被称为MKOverlayRenderer,我怎么能在我的MKMapView中添加UIImageView? Note: I know how to add an image onto the map by drawing it onto a CGContextRef, but I specifically require UIImageView to be added. 注意:我知道如何通过将图像绘制到CGContextRef上来将图像添加到地图上,但我特别要求添加UIImageView。 Thanks! 谢谢!

Here is how to add UIImage. 以下是添加UIImage的方法。 If you like to add UIImageView then the Apple documentation states ( Apple Doc MKOverlayRenderer ): 如果您想添加UIImageView,那么Apple文档说明( Apple Doc MKOverlayRenderer ):

It is recommended that you use Core Graphics to draw any content for your overlays. 建议您使用Core Graphics为叠加层绘制任何内容。 If you choose to draw using UIKit classes and methods instead, you must push the specified graphics context onto the context stack (using the UIGraphicsPushContext function) before making any drawing calls. 如果您选择使用UIKit类和方法进行绘制,则必须在进行任何绘图调用之前将指定的图形上下文推送到上下文堆栈(使用UIGraphicsPushContext函数)。 When you are done drawing, you must similarly pop the graphics context off the stack using the UIGraphicsPopContext. 完成绘制后,必须使用UIGraphicsPopContext类似地将图形上下文从堆栈中弹出。 Overlay drawing typically occurs on background threads to improve performance, so do not manipulate UIKit views or other objects that can only be manipulated on the app's main thread. 叠加绘制通常发生在后台线程上以提高性能,因此不要操纵UIKit视图或只能在应用程序主线程上操作的其他对象。

#import <Foundation/Foundation.h>

@import MapKit;

@interface MyOverlayRenderer : MKOverlayRenderer

@end

And the implementation 并实施

#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

如果您正在使用视图,则可能应该将子视图添加到MKAnnotationView

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM