1

有人知道如何解决这个问题:我在自定义 MKPolygonView 中的图像被颠倒了吗?

这个想法(这已经可以了)是有一个扩展“MKPolygonView”的类“SnowmapsOverlayView”,它显示一个图像。此图像在地图上具有默认位置和大小(在 Google Maps Web API 中充当 GroundOverlay)。这一切正常,但图像显示颠倒。我尝试了以下选项,但没有结果:

CGContextDrawImage 在传递 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;
}
4

2 回答 2

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();
}
于 2010-08-30T10:11:30.693 回答
0

您是否尝试过继承 MKOverlayView 而不是 MKPolygonView?多边形类可能会对其坐标系做一些奇怪的事情。

于 2010-08-27T18:24:34.390 回答