Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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? 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. Thanks!

like image 668
sridvijay Avatar asked Jan 26 '26 11:01

sridvijay


1 Answers

Here is how to add UIImage. If you like to add UIImageView then the Apple documentation states (Apple Doc MKOverlayRenderer):

It is recommended that you use Core Graphics to draw any content for your overlays. 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. When you are done drawing, you must similarly pop the graphics context off the stack using the 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.

#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
like image 188
y0gie007 Avatar answered Jan 29 '26 01:01

y0gie007



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!