iOS:使用核心图形创建带孔的矩形
我有一个iOS:使用核心图形创建带孔的矩形,ios,core-graphics,mapkit,Ios,Core Graphics,Mapkit,我有一个MKMapView,我想在这里灰显地图的某些部分。更具体地说,我想有一些正常显示的圆和矩形,地图的其余部分有一个半透明的灰色层。大概是这样的: 为此,我认为应该将MKOverlay和mkoverlayrender子类化。正如苹果所建议的,在我的mkoverlayrender子类中,我应该重写该方法,并使用核心图形绘制我的东西。我的问题是如何用核心图形绘制以下内容 我花了几个小时研究使用核心图形的遮罩和剪裁,但我没有发现任何类似的东西。有一些剪辑和掩蔽的示例。我想用偶数-奇数或非零卷绕
MKMapView
,我想在这里灰显地图的某些部分。更具体地说,我想有一些正常显示的圆和矩形,地图的其余部分有一个半透明的灰色层。大概是这样的:
为此,我认为应该将MKOverlay
和mkoverlayrender
子类化。正如苹果所建议的,在我的mkoverlayrender
子类中,我应该重写该方法,并使用核心图形绘制我的东西。我的问题是如何用核心图形绘制以下内容
我花了几个小时研究使用核心图形的遮罩和剪裁,但我没有发现任何类似的东西。有一些剪辑和掩蔽的示例。我想用偶数-奇数或非零卷绕数规则剪裁对我来说都不起作用,因为矩形和圆是动态的。我想我得想办法做个面具,但我不知道怎么做。四重奏。如何使用矩形和圆形创建遮罩?我还有别的办法吗
谢谢您应该能够设置一个透明的上下文,在不抚摸的情况下绘制瘦矩形和圆,在整个上下文周围绘制一个矩形边框,然后用较暗的颜色填充形状。您需要查看以确保填充的是较大的空间,而不是较小的连接形状 我想用偶数-奇数或非零卷绕数规则剪裁对我来说都不起作用,因为矩形和圆是动态的
这不重要 嗯,我自己解决的。我必须创造我自己的面具。这是通过绘制“孔”形状,将其输出到ImageRef,并反转该ImageRef以输出遮罩来完成的。如果您将其添加到项目->
QuartzClipping.m
->QuartzClippingView
类的drawInContext
方法的末尾,下面是一个代码片段
-(void)drawInContext:(CGContextRef)context {
// ...
CGContextSaveGState(context);
// dimension of the square mask
int dimension = 20;
// create mask
UIGraphicsBeginImageContextWithOptions(CGSizeMake(dimension, dimension), NO, 0.0f);
CGContextRef newContext = UIGraphicsGetCurrentContext();
// draw overlapping circle holes
CGContextFillEllipseInRect(newContext, CGRectMake(0, 0, 10, 10));
CGContextFillEllipseInRect(newContext, CGRectMake(0, 7, 10, 10));
// draw mask
CGImageRef mask = CGBitmapContextCreateImage(UIGraphicsGetCurrentContext());
UIGraphicsEndImageContext();
// the inverted mask is what we need
CGImageRef invertedMask = [self invertMask:mask dimension:dimension];
CGRect rectToDraw = CGRectMake(210.0, height - 290.0, 90.0, 90.0);
// everything drawn in rectToDraw after this will have two holes
CGContextClipToMask(context, rectToDraw, invertedMask);
// drawing a red rectangle for this demo
CGContextFillRect(context, rectToDraw);
CGContextRestoreGState(context);
}
// taken from the QuartzMaskingView below
- (CGImageRef)invertMask:(CGImageRef)originalMask dimension:(int)dimension{
// To show the difference with an image mask, we take the above image and process it to extract
// the alpha channel as a mask.
// Allocate data
NSMutableData *data = [NSMutableData dataWithLength:dimension * dimension * 1];
// Create a bitmap context
CGContextRef context = CGBitmapContextCreate([data mutableBytes], dimension, dimension, 8, dimension, NULL, (CGBitmapInfo)kCGImageAlphaOnly);
// Set the blend mode to copy to avoid any alteration of the source data
CGContextSetBlendMode(context, kCGBlendModeCopy);
// Draw the image to extract the alpha channel
CGContextDrawImage(context, CGRectMake(0.0, 0.0, dimension, dimension), originalMask);
// Now the alpha channel has been copied into our NSData object above, so discard the context and lets make an image mask.
CGContextRelease(context);
// Create a data provider for our data object (NSMutableData is tollfree bridged to CFMutableDataRef, which is compatible with CFDataRef)
CGDataProviderRef dataProvider = CGDataProviderCreateWithCFData((__bridge CFMutableDataRef)data);
// Create our new mask image with the same size as the original image
return CGImageMaskCreate(dimension, dimension, 8, 8, dimension, dataProvider, NULL, YES);
}
欢迎任何更简单/更有效的解决方案:)我认为这很重要。如果我有两个重叠的圆呢?我不希望它们的任何部分都充满色彩。使用“非零绕组编号”和“奇偶”规则,重叠部分将被填充。我想我必须画我的定制面具。