最终我正在开发一个在 iPhone 上使用的方框模糊功能。
该函数将获取 UIImage 并首先在侧面绘制透明副本,然后获取该图像并在上方和下方绘制透明副本,返回一个非常模糊的图像。
阅读Drawing with Quartz 2D Programming Guide ,建议使用 CGLayers 进行此类操作。
指南中的示例代码对我来说有点难以理解,所以我希望有人向我展示一个非常简单的示例,即获取 UIImage 并将其转换为 CGLayer 然后我会绘制副本并作为 UIImage 返回。
如果值是硬编码的(为了简单起见),那就没问题了。这只是为了让我思考一下,而不是为了生产代码。
最佳答案
UIImage *myImage = …;
CGLayerRef layer = CGLayerCreateWithContext(destinationContext, myImage.size, /*auxiliaryInfo*/ NULL);
if (layer) {
CGContextRef layerContext = CGLayerGetContext(layer);
CGContextDrawImage(layerContext, (CGRect){ CGPointZero, myImage.size }, myImage.CGImage);
//Use CGContextDrawLayerAtPoint or CGContextDrawLayerInRect as many times as necessary. Whichever function you choose, be sure to pass destinationContext to it—you can't draw the layer into itself!
CFRelease(layer);
}
从技术上讲,这是我的第一个 iPhone 代码(我只在 Mac 上编程),所以要小心。不过,我以前使用过 CGLayer,据我所知,Quartz 在 iPhone 上也没有什么不同。
… and return as a UIImage.
我不确定如何完成这部分,因为我从未使用过 UIKit。
关于iphone - 如何使用 CGLayer 在屏幕外绘制多个图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1287701/