2

我正在尝试使用 MKOverlayView 将 png 图像添加为自定义地图。我快到了 - 我能够将图像排列在正确的位置,并且我知道 MKOverlayView 子类中的 -drawMapRect: 方法正在定期调用;我似乎无法正确渲染图像。它完全模糊,几乎无法辨认。我也知道图像足够大(它是 1936 × 2967)。这是我的 -drawMapRect 代码:

- (void)drawMapRect:(MKMapRect)mapRect zoomScale:(MKZoomScale)zoomScale inContext:(CGContextRef)context{


    // Load image from applicaiton bundle
    NSString* imageFileName = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:@"map.jpg"];
    CGDataProviderRef provider = CGDataProviderCreateWithFilename([imageFileName UTF8String]);
    CGImageRef image = CGImageCreateWithJPEGDataProvider(provider, NULL, true, kCGRenderingIntentDefault);
    CGDataProviderRelease(provider);

    // save context before screwing with it
    CGContextSaveGState(context);
    CGContextScaleCTM(context, 1.0, -1.0);
    CGContextSetAlpha(context, 1.0);

    // get the overlay bounds
    MKMapRect theMapRect = [self.overlay boundingMapRect];
    CGRect theRect = [self rectForMapRect:theMapRect];

    // Draw image
    CGContextDrawImage(context, theRect, image);
    CGImageRelease(image);
    CGContextRestoreGState(context);

有谁知道发生了什么?

谢谢!-马特

4

3 回答 3

4

我有一个类似的问题。问题是我的 boundingMapRect 定义不正确。当图块上的比例很小时,整个图像会按比例缩小。然后地图被缩放,并不是所有的图像瓦片都在 boundingMapRect 瓦片中,因此它们不会以正确的比例重新绘制,并且缩小的版本会被缩放。至少我认为会发生这种情况。

希望这可以帮助。

于 2010-11-26T13:59:45.267 回答
2

摆脱 CGContextScaleCTM(context, 1.0, -1.0); 并在预览中对您的图像进行垂直翻转。mapkit 似乎使用上下文信息来确定要更清晰地渲染图像的哪个部分。知道它已经有一段时间了,但希望它有所帮助!

于 2011-01-13T10:19:22.073 回答
0

谢谢罗布,你让我很开心。更换时,我的模糊叠加图像变得清晰

CGContextScaleCTM(context, 1.0, -1.0);

CGAffineTransform flipVertical = CGAffineTransformMake(1, 0, 0, -1, 0, theRect.size.height);
CGContextConcatCTM(context, flipVertical);  
于 2016-11-27T21:18:15.593 回答