2013-07-18 51 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