我有一个像这样创建的CGLayer:
CGSize size = CGSizeMake(500, 500);
UIGraphicsBeginImageContext(tamanho);
ctx = UIGraphicsGetCurrentContext();
[self loadImageToCTX]; // this method loads an image into CTX
lineLayer = CGLayerCreateWithContext (ctx, size, NULL);
现在我有一个包含alpha和一些内容的PNG。我需要将这个PNG加载到lineLayer中,所以我做...
// lineLayer is empty, lets load a PNG into it
CGRect superRect = CGRectMake(0,0, 500, 500);
CGContextRef lineContext = CGLayerGetContext (lineLayer);
CGContextSaveGState(lineContext);
// inverting Y, so image will not be loaded flipped
CGContextTranslateCTM(lineContext, 0, -500);
CGContextScaleCTM(lineContext, 1.0, -1.0);
// CGContextClearRect(lineContext, superRect);
UIImage *loaded = [self recuperarImage:@"LineLayer.png"];
CGContextDrawImage(lineContext, superRect, loaded.CGImage);
CGContextRestoreGState(lineContext);
如果我渲染,此时,ctx + lineLayer的内容,最终图像只包含ctx ......
// if I render the contents to a view using the lines below, I see just CTX, lineLayer contents are not there
// remember CTX has an image and lineLayer has a transparent loaded PNG
// but when I render this, the final image contains just CTX's contents...
// this is how it is rendered.
CGContextDrawLayerInRect(ctx, superRect, lineLayer);
myView.image = UIGraphicsGetImageFromCurrentImageContext();
我错过了什么吗?提前谢谢。
答案 0 :(得分:1)
我不完全确定这条线在做什么:
lineLayer = CGLayerCreateWithContext (ctx, size, NULL);
为什么要重用ctx
?我的阅读是它意味着lineContext == ctx,所以对CGContextDrawLayerInRect()的调用将上下文的内容绘制到自身中,这可能不是很好(并且可能无法正确处理)。
还值得检查“已加载”是否为零。
此外,绘制图像需要做很多工作。做一些像
这样的事情UIGraphicsBeginImageContext(tamanho);
ctx = UIGraphicsGetCurrentContext();
[self loadImageToCTX];
[[UIImage imageNamed:@"LineLayer.png"] drawInRect:(CGRect){{0,0},{500,0}}];
myView.image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
答案 1 :(得分:0)
我想出了问题。 线条
CGContextTranslateCTM(lineContext, 0, -500);
CGContextScaleCTM(lineContext, 1.0, -1.0);
顺序错误...您必须在翻译之前进行缩放,否则图层将在上下文区域之外结束...
谢谢你们!