簡體   English   中英

使用CGPattern填充MKPolygon時,多邊形疊加相互干擾(使用Quartz 2D)

[英]Polygon overlays interfering with each other when filling a MKPolygon with a CGPattern (using Quartz 2D)

背景

在iOS6中,我曾經在MKMapView上添加了幾個MKPolygon (疊加層),並為mapView:viewForOverlay:提供了一個特定的MKOverlayView mapView:viewForOverlay: callback(參見MKMapViewDelegate類參考 )。 這個特定視圖的工作是使用Quartz 2D用自定義圖案填充多邊形。 它做得很好。

現在,這似乎不再像以前那樣在iOS7上運行了。

因為在iOS SDK7以及MKOverlayView及其子類中不推薦使用mapView:viewForOverlay:我嘗試切換到mapView:rendererForOverlay:並且沒有成功地MKOverlayRenderer :遇到的問題是相同的。

因此,以下示例代碼將使用MKOverlayView,但您可以在下面的代碼中輕松地使用渲染器/渲染器替換視圖/視圖,並獲得相同的效果。

遇到問題

我已將問題減少到能夠重現它的最小代碼示例,結果如下:

在iOS6上(正如預期的那樣): 在iOS6上(如預期的那樣)

在iOS7上(不是預期的): 在iOS7上(不是預期的)

我希望我的多邊形填充,總是使用相同的模式,並且模式的大小在屏幕上保持不變,與地圖的縮放級別無關。

唉,在iOS7上,當在地圖上添加多個疊加時,屏幕上的圖案尺寸會減小,同時縮小多邊形的某些部分。 只有在以最大縮放級別放大時,圖案才會獲得正確的尺寸。

問題沒有出現:

  • 僅添加一個疊加層時
  • 當覆蓋層間隔良好時
  • 對於索引最低的疊加層(使用mapView:insertOverlay:atIndex:添加多個疊加層時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:我試圖通過將它包裝在公共隊列上的dispatch_sync調用中來使fillPath的代碼線程安全,但它不能解決問題。

編輯#2:在iOS 7.0.3中未修復

問題實際上是一個錯誤,並從iOS 7.1解決

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM