2010-08-27 95 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?多邊形類可能會對其座標系統做些奇怪的事情。