2010-08-27 69 views
1

有人知道该怎么做这个问题:我的图像在自定义MKPolygonView翻转颠倒?iPhone MapKit:带图像的自定义MKPolygonView绘制倒过来

这个想法(这是工作正常)已经有一个类“SnowmapsOverlayView”,扩展“MKPolygonView”,显示图像。此图片在地图上的默认位置为&(用作Google Maps Web API中的GroundOverlay)。这一切工作正常,但图像倒过来显示。我tryed以下选项,但没有结果:

CGContextDrawImage draws image upside down when passed UIImage.CGImage

感谢任何帮助或建议!

我的代码:

#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; 
} 
+0

你刚帮我解决了一个大问题..谢谢! – 2011-05-27 10:54:19

回答

8

固定!这是完成工作的代码:

-(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(); 
} 
+0

UIImage方法在绘图时考虑了方向,CGImage方法没有。 – 2014-03-04 10:42:06

0

你有没有试着子类MKOverlayView代替MKPolygonView?多边形类可能会对其坐标系统做些奇怪的事情。