2011-05-05 29 views
8

使用CoreGraphics(在我的drawRect方法中),我試圖將混合模式應用於圖像(透明png),然後調整結果的alpha值。我假設這需要分兩步完成,但我可能是錯的。這裏是我到目前爲止(工作正常):iOS:使用CoreGraphics進行2級圖像處理

CGContextRef context = UIGraphicsGetCurrentContext(); 

CGContextSaveGState(context); 

//SET COLOR - EDIT... added a more practical color example 
CGContextSetRGBFillColor(context, 0.0, 1.0, 0.0, 1); 

//flips drawing context (apparently this is necessary) 
CGContextTranslateCTM(context, 0.0, self.bounds.size.height); 
CGContextScaleCTM(context, 1.0, -1.0);//flip context 

//DRAW PIN IMAGE 
UIImage *pin = [UIImage imageNamed:@"pin"]; 
CGRect pinrect = CGRectMake(12, 17, 25, 25);  
CGContextDrawImage(context, pinrect, pin.CGImage);//draws image in context 

//Apply blend mode 
CGContextSetBlendMode(context, kCGBlendModeColor); 
CGContextClipToMask(context, pinrect, pin.CGImage); // restricts drawing to within alpha channel 

//fills context with mask, applying blend mode 
CGContextFillRect(context, pinrect); 

CGContextRestoreGState(context); 

// -- Do something here to make result 50% transparent ?? -- 

我假設我需要繪製這一切變成某種單獨的上下文的某個地方,叫CGContextSetAlpha(...),然後重新繪製回我原始上下文,但我不知道如何。在我的最終CGContextFillRect之前設置alpha值只會改變混合模式的應用量,而不是整個圖像的alpha值。

編輯:截圖貼

enter image description here

在此先感謝。

+1

如果您在繪製圖像之前調用CGContextSetAlpha,該怎麼辦?繪製圖像之後但在應用混合之前,您可能還想將其重置爲1。 – ughoavgfhw 2011-05-05 22:09:12

+0

謝謝,但不幸的是,這會淡化圖像以及混合模式應用的數量。即。這將是50%透明,50%轉移到綠色。我希望它100%轉向綠色,然後是50%透明。 – Chazbot 2011-05-07 17:19:02

+0

我仍然沒有得到結果應該是什麼樣子,你能提供一些圖片嗎? – 2011-05-07 17:30:00

回答

9

使用透明度圖層,可以將混合應用於以100%繪製的圖像,並將結果顯示爲50%。結果如下所示:
Image showing output 我使用了帶紋理的背景,以便您可以清楚地看到較低圖像對所有內容都是50%透明,而不僅僅是以前的嘗試中的其他圖像。這裏是代碼:

CGContextRef context = UIGraphicsGetCurrentContext(); 

CGContextTranslateCTM(context, 0.0, self.bounds.size.height); 
CGContextScaleCTM(context, 1.0, -1.0);//flip context 

CGRect fullImageRect = (CGRect){42,57,100,100}; 
CGRect transparentImageRect = (CGRect){12,17,100,100}; 
CGContextSetRGBFillColor(context, 0.0, 1.0, 0.0, 1); 

// Draw image at 100% 
UIImage *testImage = [UIImage imageNamed:@"TestImage"]; 
CGContextDrawImage(context,fullImageRect,testImage.CGImage); 

// Set 50% transparency and begin a transparency layer. Inside the transparency layer, the alpha is automatically reset to 1.0 
CGContextSetAlpha(context,0.5); 
CGContextBeginTransparencyLayer(context, NULL); 
// Draw the image. It is viewed at 100% within the transparency layer and 50% outside the transparency layer. 
CGContextDrawImage(context, transparentImageRect, testImage.CGImage); 
// Draw blend on top of image 
CGContextClipToMask(context, transparentImageRect, testImage.CGImage); 
CGContextSetBlendMode(context, kCGBlendModeColor); 
CGContextFillRect(context, transparentImageRect); 
// Exit transparency layer, causing the image and blend to be composited at 50%. 
CGContextEndTransparencyLayer(context); 

編輯:刪除舊內容,因爲它佔用了大量的空間,並沒有幫助。查看修訂歷史,如果你想看到它。

+0

謝謝你一起扔這一切。在上面的例子中,你是正確的;底部圖像是以50%的圖像和100%的圖像繪製的,但它看起來好像對粉紅色+藍色圓圈只有50%透明。換句話說,50%的灰色背景不會通過底部圖像顯示,這是我需要的。頂圈是這樣做的,但它的色差不是100%。對不起,要成爲細節的忠實粉絲,並再次感謝您的幫助。 – Chazbot 2011-05-07 20:00:20

+0

好吧,現在我明白你想要什麼了,我想出瞭如何去做。您需要使用透明度圖層。我將修改我的帖子以顯示此內容。 – ughoavgfhw 2011-05-07 20:20:34

+0

令人驚歎。謝謝! – Chazbot 2011-05-07 20:41:05