iphone - 如何使用 CGLayer 在屏幕外绘制多个图像

标签 iphone core-graphics cglayer

最终我正在开发一个在 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/

相关文章:

ios - 创建可点击的 CGPath

ios - 用渐变描边画一个圆

ios - 我想使用 JSON 和 google maps SDK 放置多个标记

iphone - 指定单元格 imageView 的宽度

macos - CGFontGetGlyphWithGlyphName() 出现问题

objective-c - objective-c - 带圆角的 UINavigationBar

iphone - 优化 Core Graphics 动画绘图 (iPhone)

ios - 更改突出显示的 UIButton BG 颜色,但保留层角半径?

iphone - 如何自定义 EKEventEditViewController

iphone - 在不使用 iphone 中的设备灰度的情况下将图像转换为黑白图像