背景
在iOS6中,我曾经在MKMapView上添加了几个MKPolygon
(叠加层),并为MKOverlayView
回调提供了一个特定的mapView:viewForOverlay:
(请参阅MKMapViewDelegate Class Reference)。这个特定视图的工作是使用Quartz 2D用自定义图案填充多边形。它做得很好。
现在,这似乎不再像以前那样在iOS7上运行了。
由于iOS SDK7以及mapView:viewForOverlay:
及其子类中不推荐使用MKOverlayView
,因此我尝试切换到mapView:rendererForOverlay:
并继承MKOverlayRenderer
但未成功:遇到的问题是相同。
以下示例代码将使用MKOverlayView,但您可以在下面的代码中轻松地使用渲染器/渲染器替换视图/视图,并获得相同的效果。
遇到问题
我已将问题减少到能够重现它的最小代码示例,结果如下:
在iOS6上(正如预期的那样):
在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中未修复
答案 0 :(得分:0)
问题实际上是一个错误,并从iOS 7.1解决