iPhone MapKit: Custom MKPolygonView with image draws upside down

北战南征 提交于 2019-12-11 13:56:46

问题


does anybody know what to do with this problem: my image in a custom MKPolygonView is flipped upside down?

The idea (this is working OK already) is having an class "SnowmapsOverlayView" that extends "MKPolygonView", that displays a image. This image has a default location & size on the map (acts as a GroundOverlay in the Google Maps web API). This is all working fine, but the image is displayed upside down. I've tryed the following options, but with no result:

CGContextDrawImage draws image upside down when passed UIImage.CGImage

Thanks for any help or suggestions!

My code:

#import <MapKit/MapKit.h>

@interface SnowmapsOverlayView : MKPolygonView
{
}

@end

-----------------        

#import "SnowmapsOverlayView.h"

@implementation SnowmapsOverlayView

-(id)initWithPolygon:(MKPolygon *)polygon
{
    self = [super initWithPolygon:polygon];

    return self;    
}

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

    //This should do the trick, but is doesn't.. maybe a problem with the "context"?
    //CGRect imageRect = CGRectMake(0, 0, image.size.width, image.size.height);
    //CGContextTranslateCTM(context, 0, image.size.height);
    //CGContextScaleCTM(context, 1.0, -1.0);

    CGRect overallCGRect = [self rectForMapRect:self.overlay.boundingMapRect];
    CGContextDrawImage(context, overallCGRect, image.CGImage);
}

-(BOOL)canDrawMapRect:(MKMapRect)mapRect zoomScale:(MKZoomScale)zoomScale
{
    return YES;
}

回答1:


Fixed! This is the code that gets the job done:

-(void)drawMapRect:(MKMapRect)mapRect zoomScale:(MKZoomScale)zoomScale inContext:(CGContextRef)context
{
    UIImage *image = [UIImage imageNamed:@"snowmap.png"];
    MKMapRect theMapRect = [self.overlay boundingMapRect];
    CGRect theRect = [self rectForMapRect:theMapRect];

    UIGraphicsPushContext(context);
    [image drawInRect:theRect blendMode:kCGBlendModeNormal alpha:1.0];
    UIGraphicsPopContext();
}



回答2:


Have you tried subclassing MKOverlayView instead of MKPolygonView? The polygon class may do something weird to its coordinate system.



来源:https://stackoverflow.com/questions/3585563/iphone-mapkit-custom-mkpolygonview-with-image-draws-upside-down

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!