문제
이것이 제가하려는 일입니다 : 두 개의 cglayers, a와 b를 만듭니다. b를 a에 넣고 블렌드 모드를 사용하여 결과 이미지를 가져옵니다.
이것은 코드입니다
// 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();
내가 얻는 결과는 결과적인 결과가 두 층의 구성과 같지 않다는 것입니다. 실제로 결과적인 결과는 첫 번째 층과 동일합니다. 두 번째 층은 이것에 의해 무시되고 있습니다 !!!
???
내가 뭔가를 놓치고 있습니까?
도움을 주셔서 감사합니다.
해결책
당신이 가정하고있는 것 같습니다 myImage
보다 큽니다 myObject
, 당신은 하나를 중심으로하려고하기 때문에.
당신이 만들 때 objectLayer
, 그것은 크기가 작은 것입니다 myObject
.
rect objectRect
더 큰 이미지 내에서 작은 이미지를 중심으로 할 수 있습니다.
그러나 당신이 전화 할 때 CGContextDrawImage
~에 myObject
, 너는 사용한다 objectRect
작은 레이어 내에 이미지를 배치하려면 이는 불일치로 보입니다.
어쩌면 문제는 단순히 CGContextDrawImage
전화, objectRect
두 번째 레이어를 프레임에서 배치하고 있습니다.
제휴하지 않습니다 StackOverflow