2013-07-18 64 views
2

此代码导致下面的图像。据我了解CGContextClipToMask,红色的矩形不应该是可见的,因为它是在裁剪区域之外。我在这里错过了什么?谢谢你的帮助!CGContextClipToMask不剪辑

CGContextRef context = UIGraphicsGetCurrentContext(); 
CGContextSetFillColorWithColor(context, [UIColor blackColor].CGColor); 
CGContextFillRect(context, rect); 
CGContextSetLineWidth(context, 20); 
CGContextSetStrokeColorWithColor(context, [UIColor whiteColor].CGColor); 

// draw partial circle 
UIBezierPath *arc = [UIBezierPath bezierPathWithArcCenter:center radius:radius startAngle:startAngle endAngle:endAngle clockwise:NO]; 
CGContextAddPath(context, [arc CGPath]); 
CGContextStrokePath(context); 

// create mask 
CGImageRef mask = CGBitmapContextCreateImage(context); 
self.maskCreated(mask); 

// save state 
CGContextSaveGState(context); 

// clip with mask 
CGContextClipToMask(context, rect, mask); 

// draw test rect 
CGContextSetFillColorWithColor(context, [UIColor redColor].CGColor); 
CGContextFillRect(context, CGRectMake(0, 0, 100, 100)); 

// restore state 
CGContextRestoreGState(context); 

Code Result

回答

0

其实不明白你的问题,但你可以在你的方法,像这样隐藏矩形:

// draw a test rect    
CGContextSetFillColorWithColor(context, [UIColor redColor].CGColor); 

CGRect rect = CGRectZero; 
CGContextFillRect(context, rect); 
+0

谢谢您的回答。其实,矩形不是问题。问题是,我从该弧中创建了一个遮罩,但创建遮罩后的视图和所有内容都未被遮罩。 – Thomas

0

documentationCGContextClipToMask说:

如果mask是图像,则它必须位于DeviceGray颜色空间中可能没有alpha分量,并且可能不会被图像掩码或遮罩颜色掩盖。

我假设你的代码是在UIView子类的的-drawRect:方法,让您使用的是提供给你,这是一个RGB色彩空间,并可能有一个alpha分量CGContext。您的mask图像是从该上下文创建的,因此它获得相同的属性。

要解决此问题,请使用单独的位图上下文来生成蒙版,并使用不带alpha的灰色空间。这是一个独立的例子,它可以做类似于你的代码的东西。

- (void)drawRect:(CGRect)rect 
{ 
    // Create a context for the mask 
    CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceGray(); 
    CGContextRef maskContext = CGBitmapContextCreate(NULL, rect.size.width, rect.size.height, 8, 0, colorSpace, kCGImageAlphaNone | kCGBitmapByteOrderDefault); 
    CGColorSpaceRelease(colorSpace); 

    // Fill with black 
    CGContextSetFillColorWithColor(maskContext, [UIColor blackColor].CGColor); 
    CGContextFillRect(maskContext, rect); 

    // Draw an arc in white 
    CGContextSetLineWidth(maskContext, 20); 
    CGContextSetStrokeColorWithColor(maskContext, [UIColor whiteColor].CGColor); 
    CGContextAddArc(maskContext, CGRectGetMidX(rect), CGRectGetMidY(rect), 50, M_PI, 0, false); 
    CGContextStrokePath(maskContext); 

    // Create the mask image from the context, and discard the context 
    CGImageRef mask = CGBitmapContextCreateImage(maskContext); 
    CGContextRelease(maskContext); 

    // Now draw into the view itself 
    CGContextRef context = UIGraphicsGetCurrentContext(); 

    // Apply the mask 
    CGContextClipToMask(context, rect, mask); 

    // Then draw something that overlaps the mask 
    CGContextSetFillColorWithColor(context, [UIColor redColor].CGColor); 
    CGContextFillRect(context, rect); 

    // Make sure to clean up when we're done 
    CGImageRelease(mask); 
} 
+0

(当然,一旦我完成了这个答案,我注意到这个问题已经超过2年了,噢,不管怎么样,不妨将它发布)。 –

+0

我会检查你的答案,并将其标记为正确的工作:) – Thomas