這是我想要做的:創建兩個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不等於所述兩個層的組合物。事實上,圖像與第一層相同。第二層被這個忽略!
???
我錯過了什麼?
感謝您的任何幫助。
可能只是一個錯字:objectoContext? – mahboudz 2009-09-13 08:16:30
謝謝。對於那個很抱歉。當我從Xcode複製代碼來聽到這些時,這只是一個錯字,因爲我必須將一些我用我的語言鍵入的可變語言更改爲英語,以便澄清代碼。 – SpaceDog 2009-09-13 10:09:08