簡體   English   中英

如何將UIImageView添加到MKOverlayRenderer?

[英]How to add UIImageView to MKOverlayRenderer?

既然他們在MKOverlayView中沒有更多的視圖,並且它被稱為MKOverlayRenderer,我怎么能在我的MKMapView中添加UIImageView? 注意:我知道如何通過將圖像繪制到CGContextRef上來將圖像添加到地圖上,但我特別要求添加UIImageView。 謝謝!

以下是添加UIImage的方法。 如果您想添加UIImageView,那么Apple文檔說明( 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

如果您正在使用視圖,則可能應該將子視圖添加到MKAnnotationView

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM