理想情况下,我正在尝试通过CAShapeLayer之类的方法绘制路径,并使该路径像素化为该样式。它不需要看起来像实际资产那样超级好,但是它需要遵循一般的样式。我尝试了几种方法,并且很接近,结果在这里正确无误:
我现在唯一的问题是消除应用于尝试执行此方法的模糊。我需要将结果像素化,但仍然非常清晰,这是我用于获得上述效果的最新方法:
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
}
}