2009-12-20 26 views
7

我需要繪製一系列PNG圖像的成CGContext,所以我可以融合在一起,他們說:繪製一個PNG圖像轉換成圖形上下文的混合模式操縱

CGContextRef context = UIGraphicsGetCurrentContext(); 
CGContextSetBlendMode(context, kCGBlendModeOverlay); 

目前,我有我的應用程序工作的基礎上只畫每個圖像作爲UIImageView並將它們作爲子視圖通過以下方式添加到視圖文件:[self addSubview:someImageView] ...但這不允許進行混合模式操作,對不對?

someImageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"image-name.png"]]; 

所以我已經試過,沒有運氣替換那些任一:

目前的UIImageView瓦爾正在通過分配

[[UIImage imageNamed:@"image-name.png"] drawInRect:rect blendMode:kCGBlendModeOverlay alpha:1.0]; 

而這其中需要CGImage我不知道如何使用PNG製作:

CGContextDrawImage(context, rect, someCGImage); 

那麼什麼我錯過了嗎?這些不會被任何交互觸發,只需在應用加載時加載/繪製。

感謝您的任何線索。 :)

回答

19

你不需要使用UIImageViews。 取而代之的是,在調用UIGraphicsBeginImageContextUIGraphicsEndImageContext之間包含您的繪圖。

例如(代碼沒試過):

UIImage* uiimage = [UIImage imageNamed:@"image-name.png"]; 
UIImage* uiimage2 = [UIImage imageNamed:@"other-image-name.png"]; 

CGSize size = uiimage.size; 

UIGraphicsBeginImageContext(size); 

[uiimage drawAtPoint:CGPointZero blendMode:kCGBlendModeOverlay alpha:1.0]; 
[uiimage2 drawAtPoint:CGPointZero blendMode:kCGBlendModeOverlay alpha:1.0]; 

UIImage* blendedImage = UIGraphicsGetImageFromCurrentImageContext(); 

UIGraphicsEndImageContext(); 

如果你想使用CG通話,通話:

CGContextRef context = UIGraphicsGetCurrentContext(); 

獲得上下文參考。

+1

順便說一句,從UIImage製作CGImage很容易。 UIImage上有CGImage屬性。但是,避免混合UIImage和CGImage繪圖程序,否則你會發現繪圖上下文是顛倒的。 – 2009-12-20 23:09:16

+0

我不清楚最後一部分,因爲我想在繪製它們之前旋轉uiimages,但調用CGContextRotateCTM(context,3.14);在繪圖命令實際上未旋轉期望的180度之前。 是的,我在drawRect方法中調用的第一個命令是CGContextRef context = UIGraphicsGetCurrentContext(); 想法? – steganous 2009-12-22 16:08:13

+0

這聽起來像你做對了。你可以發佈你的新代碼嗎? – 2009-12-23 15:59:56