【发布时间】:2017-12-19 04:00:47
【问题描述】:
下面的第一张图片是原始图片。
第二个图像是使用 Core Image API 实现的模糊图像。
第三张图像是使用UIVisualView 实现的模糊图像。
很明显,Core Image 模糊了图像并缩小了它。更大的半径导致更宽的白色边框。
问题:如何使用core image做一个类似UIVisualView效果的视觉效果。
至少,如何使用 Core Image 来模糊没有白色边框的图像。
func blur(image: UIImage, withRadius radius: Float) -> UIImage {
let context = CIContext(options: nil)
let image = CIImage(image: image)
let filter = CIFilter(name: "CIGaussianBlur")
filter?.setValue(image, forKey: kCIInputImageKey)
filter?.setValue(radius, forKey: "inputRadius")
let result = filter?.outputImage
return UIImage(cgImage: context.createCGImage(result!, from: (result?.extent)!)!)
}
第三张图片是
func addVisualEffectView() {
let effectView = UIVisualEffectView(effect: UIBlurEffect(style:.light))
effectView.frame = originalImageView.bounds // originalImageView is the ImageView represents the original image
originalImageView.addSubview(effectView)
}
【问题讨论】:
-
你有你的答案。现在,如果你想更好地解释下面发生的事情,我推荐“Core Image for Swift”——尤其是第 3.1 章。它解释了为什么你有一个白色边框以及如何纠正它 - 基本上,它是一个卷积过滤器,边缘是......嗯,边缘。
-
@dfd 谢谢,伙计。我会检查的。
标签: ios core-image uivisualeffectview