11

背景

在 iOS6 中,我曾经MKPolygon在 MKMapView 上添加几个(覆盖)并提供特定MKOverlayViewmapView:viewForOverlay:回调(请参阅MKMapViewDelegate 类参考)。这个特定视图的工作是使用 Quartz 2D 用自定义图案填充多边形。它确实工作得很好。

现在,这似乎不再像我以前那样在 iOS7 上工作了。

因为mapView:viewForOverlay:在iOS SDK7MKOverlayView及其子类中已弃用,所以我尝试切换到mapView:rendererForOverlay:子类并MKOverlayRenderer没有成功:遇到的问题是一样的。

因此,以下示例代码将使用 MKOverlayView,但您可以在下面的代码中轻松地将 view/View 替换为 renderer/Renderer 并获得相同的效果。

遇到的问题

我已将问题减少到能够重现它的最小代码示例,结果如下:

在 iOS6 上(如预期): 在 iOS6 上(如预期的那样)

在 iOS7 上(不像预期的那样): 在 iOS7 上(不像预期的那样)

我期望填充我的多边形,始终使用相同的图案,并且图案的大小在屏幕上保持不变,与地图的缩放级别无关。

唉,在 iOS7 上,当在地图上添加多个叠加层时,屏幕上的图案尺寸会减小,同时缩小多边形的某些部分。仅当以最大缩放级别放大时,图案才会获得正确的尺寸。

问题没有出现:

  • 仅添加一个叠加层时
  • 当叠加层间距良好时
  • 对于具有最低索引的叠加层(当添加多个叠加层时mapView:insertOverlay:atIndex:

即使仅添加了一个带图案的覆盖层以及其他未带图案的覆盖层,问题似乎也会发生。

代码示例

MTViewController.h

#import <UIKit/UIKit.h>
#import <MapKit/MapKit.h>

@interface MTViewController : UIViewController<MKMapViewDelegate>

@property (strong, nonatomic) IBOutlet MKMapView *mapView;    

@end

MTViewController.m

#import "MTViewController.h"
#import <MapKit/MapKit.h>
#import "MTPolygonWithPatternOverlayView.h"

@implementation MTViewController

- (void)dealloc {
    self.mapView.delegate = nil;
}

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        self.title = @"MapTest - CGPattern";
    }
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.mapView.delegate = self;

    // Add a 4 polygon overlays on the map
    int count = 0;
    float delta = 0.012;
    for (int i = 0; i < 4; i++) {
        CLLocationCoordinate2D coords[4];
        coords[0] = CLLocationCoordinate2DMake(58.395,15.555 + (i*delta));
        coords[1] = CLLocationCoordinate2DMake(58.390,15.560 + (i*delta));
        coords[2] = CLLocationCoordinate2DMake(58.385,15.555 + (i*delta));
        coords[3] = CLLocationCoordinate2DMake(58.390,15.550 + (i*delta));
        MKPolygon *overlay = [MKPolygon polygonWithCoordinates:coords count:4];
        [self.mapView insertOverlay:overlay atIndex:count++];
    }

    // Zoom to region containing polygons
    MKCoordinateRegion region = MKCoordinateRegionMake(CLLocationCoordinate2DMake(58.390,15.561), MKCoordinateSpanMake(0.015, 0.015));
    [self.mapView setRegion:[self.mapView regionThatFits:region] animated:NO];
}

- (MKOverlayView *)mapView:(MKMapView *)mapView viewForOverlay:(id<MKOverlay>)overlay {
    MKOverlayView *overlayView = nil;

    if ([overlay isKindOfClass:[MKPolygon class]]) {
        MTPolygonWithPatternOverlayView *polygonOverlayView = [[MTPolygonWithPatternOverlayView alloc] initWithPolygon:overlay];
        polygonOverlayView.alpha = 1.0f;
        polygonOverlayView.strokeColor = [UIColor blueColor];
        polygonOverlayView.lineWidth = 3.0f * [[UIScreen mainScreen] scale];
        overlayView = polygonOverlayView;
    }
    return overlayView;
}

@end

MTPolygonWithPatternOverlayView.h

#import <MapKit/MapKit.h>

@interface MTPolygonWithPatternOverlayView : MKPolygonView

@end

MTPolygonWithPatternOverlayView.m

#import "MTPolygonWithPatternOverlayView.h"

@implementation MTPolygonWithPatternOverlayView

void patternReleaseInfoCallback(void *info) {
}

void drawPatternCell(void *info, CGContextRef context) {
    float cellWidth = CGContextGetClipBoundingBox(context).size.width;
    float cellHeight = CGContextGetClipBoundingBox(context).size.height;

    // Make line width look constant on screen independently of map zoom level
    CGFloat lineWidth = [[UIScreen mainScreen] scale] * cellWidth / 64.0f;
    CGContextSetLineWidth(context, lineWidth);

    // Draw following pattern in cell:
    // \       /
    //  \     /
    //   \ _ /
    //    |_|
    //   /   \
    //  /     \
    // /       \

    // Draw diagonal
    CGContextSetStrokeColorWithColor(context, [UIColor redColor].CGColor);
    CGPoint points [] = {{0.0,0.0}, {cellWidth,cellHeight }, {cellWidth,0.0}, {0.0,cellHeight}};
    CGContextStrokeLineSegments(context, points, 4);

    // Draw middle square
    CGContextSetFillColorWithColor(context, [UIColor blackColor].CGColor);
    float partWidth = cellWidth / 8;
    CGRect middleSpot = CGRectMake(partWidth * 3, partWidth*3, 2* partWidth, 2*partWidth);
    CGContextFillRect(context, middleSpot);
}

- (void)fillPath:(CGPathRef)path inContext:(CGContextRef)context {
    CGPatternCallbacks callBack;
    callBack.drawPattern = &drawPatternCell;
    callBack.releaseInfo = &patternReleaseInfoCallback;
    callBack.version = 0;

    float cellWidth = CGContextGetClipBoundingBox(context).size.width / 4;
    CGPatternRef pattern = CGPatternCreate(NULL, CGRectMake(0.0f, 0.0f, cellWidth, cellWidth), CGAffineTransformIdentity, cellWidth, cellWidth, kCGPatternTilingConstantSpacing, true, &callBack);

    CGContextSaveGState(context);
    CGColorSpaceRef  patternSpace = CGColorSpaceCreatePattern (NULL);
    CGContextSetFillColorSpace (context, patternSpace);
    CGContextAddPath(context, path);
    float alpha = 1;
    CGContextSetFillPattern(context, pattern, &alpha);
    CGContextDrawPath(context, kCGPathFill);
    CGContextRestoreGState(context);

    CGColorSpaceRelease (patternSpace);
    CGPatternRelease(pattern);
}

@end

问题

我正在将其报告为 Apple 的错误跟踪器上的错误,但我想先检查其他人是否发现我在使用 MapKit 和 Quartz 2D 时存在缺陷。

编辑#1:我试图fillPath通过将它包装在一个公共队列上的调用中来使其代码线程安全,dispatch_sync但它不能解决问题。

编辑 #2:在 iOS 7.0.3 中未修复

4

1 回答 1

0

问题实际上是一个错误并从 iOS 7.1 解决

于 2014-03-13T15:02:15.060 回答