Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/18.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 过滤器不工作_Ios_Swift_Core Image - Fatal编程技术网

Ios 过滤器不工作

Ios 过滤器不工作,ios,swift,core-image,Ios,Swift,Core Image,我正在尝试使用以下代码向图层添加模糊效果。 但它不起作用,有什么建议吗 let layer = CALayer() layer.frame = CGRect(x: 150, y: 150, width: 150, height: 150) layer.backgroundColor = UIColor.blue.cgColor layer.contents = UIImage(named: "D1")?.cgImage if let filter = CIFilter(name: "CIGau

我正在尝试使用以下代码向图层添加模糊效果。
但它不起作用,有什么建议吗

let layer = CALayer()
layer.frame = CGRect(x: 150, y: 150, width: 150, height: 150)
layer.backgroundColor = UIColor.blue.cgColor
layer.contents = UIImage(named: "D1")?.cgImage

if let filter = CIFilter(name: "CIGaussianBlur", withInputParameters: ["inputRadius": 15]) {
filter.setDefaults()
layer.filters = [filter]
}
view.layer.addSublayer(layer)

它只是显示图像,没有过滤效果。我真的很困惑。谢谢您的建议。

这在iOS上不起作用。从文件中:

特殊注意事项

iOS中的层不支持此属性

您需要按照以下方式进行操作:


请注意,我的Swift 4代码未经测试。可能存在一个或两个构建问题(特别是展开问题),并且(当然)由于这一点,
nil
可能会在运行时发生。我提供的Swift代码很好地翻译了上面链接的问题中已接受的答案。

如果您注释掉filter.setDefaults(),会发生什么情况?@Louislung无论是否使用
filter.setDefaults()
。是的,这都非常有效!请注意,使用此方法,模糊会在内容周围创建一个模糊边界,距离大约为您输入的输入半径。。。我用补丁解决方案解决了这个问题(在框架中考虑到了这一点…)
let ciInput = CIImage(image: UIImage(named: "D1")!)
let filter = CIFilter(name: "CIGaussianBlur", withInputParameters: ["inputImage": ciInput, "inputRadius": 15])
let ciOutput = filter?.outputImage
let ciContext = CIContext(options: nil)
let cgImage = ciContext.createCGImage(ciOutput!, from: (ciOutput?.extent)!)
layer.contents = cgImage