2010-09-16 72 views
3

我想將r,g,b和兩個CGImages的值相乘,將單個圖像用作蒙版(在其Alpha通道中)和色調(在rgb通道中)。我第一次嘗試簡單:將RGBA與兩個CGImageRefs相乘

CGContextDrawImage(context, CGRectMake(0, 0, _w, _h), handle()); 
CGContextSetBlendMode(context, kCGBlendModeMultiply); 
CGContextDrawImage(context, CGRectMake(0, 0, other->width(), other->height()), over); 

但是,這只是色彩,並沒有掩蓋。因此,我提取從掩蔽圖像的阿爾法成CGImage掩模灰度圖像(使用CGDataProvider),並用於隨着所述上下文的掩模:

// "mask" is "other"'s alpha channel as an inverted grayscale image 
CGContextSaveGState(context); 
CGContextClipToMask(context, CGRectMake(0, 0, other->width(), other->height()), mask); 
CGImageRelease(mask); 
CGContextDrawImage(context, CGRectMake(0, 0, _w, _h), handle()); 
CGContextRestoreGState(context); // Don't apply the clip mask to 'other', since it already has it 
CGContextSetBlendMode(context, kCGBlendModeMultiply); 
CGContextDrawImage(context, CGRectMake(0, 0, other->width(), other->height()), other->handle()); 

它仍然看起來不正確,雖然。圖像獲得更輕;一個倍增的圖像應該總是至少較暗?在圖片預覽:提前啓蒙:)

http://dl.dropbox.com/u/6775/multiply/index.html

感謝

回答

2

的第一件事情是,你不需要alpha通道解壓縮到一個單獨的通道做屏蔽。你可以用kCGBlendModeDestinationIn來繪製它。對於倒轉面具,同樣的事情,但kCGBlendModeDestinationOut

因此,解決方法是用Multiply繪製第二個圖像,然後再用Destination In或Out繪製它。

+0

這是一個*優秀*的解決方案,但我只是意識到爲什麼我從來沒有考慮過它:這個應用程序必須支持10.4 :(:(我將它標記爲正確,因爲它*是*正確的所述問題) – nevyn 2010-09-20 07:29:06