2011-10-17 43 views
1

我試圖用iOS中的觸摸擦除圖像。通過設置混合模式爲kCGBlendModeClear,我可以做到這一點 - 但只有硬邊緣。我可以使用不同的線寬和alpha來繪製筆畫,但似乎CGContextSetAlpha對kCGBlendModeClear沒有影響。如何在CGBitmapContext中實現柔軟的橡皮擦筆觸

我該如何解決這個問題?透明層,前/後

CGPathRef pathToErase = ...; // The path you want erased 
// could also be an image or (nearly) anything else 
// that can be drawn in a bitmap context 

CGContextSetBlendMode(ctx, kCGBlendModeDestinationOut); 
CGContextBeginTransparencyLayer(ctx, NULL); 
{ 
    CGContextSetGrayFillColor(ctx, 0.0, 1.0); // solid black 

    CGContextAddPath(ctx, pathToErase); 
    CGContextFillPath(ctx); 
    // the above two lines could instead be CGContextDrawImage() 
    // or whatever else you're using to clear 
} 
CGContextEndTransparencyLayer(ctx); 

請注意,您還應該保存和恢復gstate(CGContextSaveGState()/CGContextRestoreGState()):

回答

2

我會用一個透明層合成與kCGBlendModeDestinationOutDa * (1 - Sa), Dc * (1 - Sa)。)像這樣的東西確保混合模式和任何其他gstate更改不會持續。

注意:這是大腦編譯的,透明層可能不適合所有的混合模式。如果是,請嘗試將路徑/圖像繪製到第二個位圖上下文中,然後使用上述混合模式繪製該上下文的內容。

您還可以嘗試其他混合模式以獲得不同的效果。

+0

謝謝,這工作得很好 - 我正在編輯kCGBlendModeDestinationOut – user441669