2009-09-13 75 views
0

這是我想要做的:創建兩個CGLayers,A和B.把B放在A上,使用混合模式,得到最終的圖像。CGLayers不混合

這是代碼

// Begin the drawing 
CGLayerRef objectLayer; 
CGContextRef fundoContext, objectContext; 
CGRect backRect, objectRect; 

CGFloat w = myImage.size.width; 
CGFloat h = myImage.size.height; 
CGSize sz = CGSizeMake(w, h); 

backRect = CGRectMake (0, 0, h, w); 

UIGraphicsBeginImageContext(sz); 
CGContextRef context = UIGraphicsGetCurrentContext(); 
fundoLayer = CGLayerCreateWithContext (context, sz, NULL); 
fundoContext = CGLayerGetContext (fundoLayer); 

CGContextDrawImage(fundoContext, backRect, myImage.CGImage); 
CGContextDrawLayerAtPoint(context, CGPointMake(0,0), fundoLayer); 



UIImage *myObject = [[UIImage alloc]initWithContentsOfFile:[[NSBundle mainBundle] 
pathForResource:@"image" ofType:@"png"]]; 


w = myObject.size.width; 
h = myObject.size.height; 
sz = CGSizeMake(w, h); 

float centroW, centroH; 

// centering 
objectRect = CGRectMake (((myImage.size.width - w)/2), 
((myImage.size.height - h)/2), w, h); 

centroW = myImage.size.width/2; 
centroH = myImage.size.height/2; 


//objectRect = CGRectMake (0, 0, h, h); 
objectLayer = CGLayerCreateWithContext (context, sz, NULL); 
objectContext = CGLayerGetContext (objectLayer); 

// in theory this should set the blendmode of the top layer to screen… 
CGContextSetBlendMode(objectContext, kCGBlendModeScreen); 


CGContextDrawImage(objectContext, objectRect, myObject.CGImage); 
CGContextDrawLayerAtPoint(context, CGPointMake(centroW, centroH), objectLayer); 

resultingImage = UIGraphicsGetImageFromCurrentImageContext(); 

結果我得到的是resultingImage不等於所述兩個層的組合物。事實上,圖像與第一層相同。第二層被這個忽略!

???

我錯過了什麼?

感謝您的任何幫助。

+0

可能只是一個錯字:objectoContext? – mahboudz 2009-09-13 08:16:30

+0

謝謝。對於那個很抱歉。當我從Xcode複製代碼來聽到這些時,這只是一個錯字,因爲我必須將一些我用我的語言鍵入的可變語言更改爲英語,以便澄清代碼。 – SpaceDog 2009-09-13 10:09:08

回答

1

看起來您假設myImage大於myObject,因爲您正試圖將其中一個放在另一箇中。

當您創建objectLayer時,它的尺寸較小 - 即myObject

矩形objectRect被放置在較小的圖像的中心。

但是,當你在myObjectCGContextDrawImage,您使用objectRect較小層中的圖像位置 - 這似乎是一個不匹配。

所以也許麻煩在於,在最後的CGContextDrawImage調用中,objectRect將第二層定位在幀外。

+0

我的男人!!!!!!!!!!!!!!謝謝!經過幾個小時看代碼,你看不到這樣的事情! – SpaceDog 2009-09-13 11:49:42