绘制像素化线

问题描述 投票:2回答:1

我正在尝试绘制看起来像这种样式的“像素化”或“矢量化”线:enter image description hereenter image description here

理想情况下,我正在尝试通过CAShapeLayer之类的方法绘制路径,并使该路径像素化为该样式。它不需要看起来像实际资产那样超级好,但是它需要遵循一般的样式。我尝试了几种方法,并且很接近,结果在这里正确无误:enter image description here

我现在唯一的问题是消除应用于尝试执行此方法的模糊。我需要将结果像素化,但仍然非常清晰,这是我用于获得上述效果的最新方法:

let path = UIBezierPath(roundedRect: bounds, cornerRadius: 20)
let lay = CAShapeLayer()
lay.path = path.cgPath
lay.strokeColor = UIColor.green_bright.cgColor
lay.lineWidth = 5
lay.fillColor = nil
lay.shouldRasterize = true
lay.rasterizationScale = 0.1
layer.addSublayer(lay)

我也尝试过使用CIFilters,例如CIPixellate,但无法弄清楚如何将这种过滤器以任何方式应用于CAShapeLayer或路径。

是否有任何方法可以清除此方法,也可以通过其他任何方法来实现这种效果?感谢所有反馈或想法。

更新:我正在尝试通过将图层变成图像并应用CIFilter来使用过滤器,这是我尝试失败的方式:

extension CAShapeLayer {
    func apply(_ filter: String) -> UIImage? {
        let context = CIContext(options: nil)
        let renderer = UIGraphicsImageRenderer(size: frame.size)
        if let currentFilter = CIFilter(name: filter) {
            let image = renderer.image { context in
                render(in: context.cgContext)
            }
            let beginImage = CIImage(image: image)
            currentFilter.setValue(beginImage, forKey: kCIInputImageKey)

            if let output = currentFilter.outputImage {
                if let cgimg = context.createCGImage(output, from: output.extent) {
                    return UIImage(cgImage: cgimg)
                }
            }
        }
        return nil
    }
}
ios uikit core-graphics uibezierpath cashapelayer
1个回答
1
投票

如果这是您的意思...

enter image description here

...然后我认为您使用CIPixellate CIFilter的想法是正确的,因为这就是我得到该图像的方式。步骤是:

  1. [使用形状图层构造形状,基本上就像已经做的一样。

  2. 使图形图像上下文并在其中渲染形状层。现在,您已经在形状图层之外制作了一个UIImage。

  3. 将UIImage放入CIImage并通过CIPixellate过滤器传递。

  4. 重新创建图形图像上下文;将UIImage围绕过滤器的CIImage输出包装,并将其绘制到图像上下文中。现在,您已将滤镜输出呈现为新的像素化UIImage。

  5. 显示结果UIImage。

© www.soinside.com 2019 - 2024. All rights reserved.