我使用了一些解决方法来成功保存带有蒙版的图像(因为您无法使用 CoreAction 渲染蒙版)。
这是我的代码:
CGContextRef context = CGBitmapContextCreate(nil, cWidth, cHeight, bitsPerComponent, bytesPerRow, CGColorSpaceCreateDeviceRGB(), CGImageGetBitmapInfo(image));
//the mask:
CGContextClipToMask(context, CGRectMake(0,0,1280,935), self.image.image.CGImage);
//the image to mask:
CGContextDrawImage(context, CGRectMake(0, 0, 1280,935), viewImage.CGImage);
CGImageRef mergeResult = CGBitmapContextCreateImage(context);
saver = [[UIImage alloc] initWithCGImage:mergeResult];
所以这效果很好, mask 会剪掉目标图像中其形状之外的所有内容。这使得周围区域和背景变白。我不想显示白色,而是想显示图像/颜色/图案等。所以我基本上想在所有这些后面堆叠另一个图像。
如何做到这一点?谢谢
最佳答案
首先将背景绘制到上下文中,然后进行剪辑,然后将图像绘制到相同的上下文中。
关于iphone - 如何在 CGContext 上分层,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9623156/