全屏高斯模糊

Gaussian blur on full screen

我想对 iOS 应用程序的整个屏幕进行模糊处理,但我无法使用 UIBlurEffect,因为我希望能够控制模糊度。所以我正在尝试使用 CIGaussianBlur,但我在处理屏幕边缘时遇到了问题。

我正在截取屏幕截图,然后 运行 通过 CIFilterCIGaussianBlur 将其 CIImage 转换回 UIImage,并在屏幕顶​​部添加新的模糊图像。

这是我的代码:

    let layer = UIApplication.sharedApplication().keyWindow?.layer
    UIGraphicsBeginImageContext(view.frame.size)

    layer!.renderInContext(UIGraphicsGetCurrentContext()!)
    let screenshot = UIGraphicsGetImageFromCurrentImageContext()
    UIGraphicsEndImageContext()

    let blurRadius = 5
    var ciimage: CIImage = CIImage(image: screenshot)!
    var filter: CIFilter = CIFilter(name:"CIGaussianBlur")!
    filter.setDefaults()
    filter.setValue(ciimage, forKey: kCIInputImageKey)
    filter.setValue(blurRadius, forKey: kCIInputRadiusKey)

    let ciContext = CIContext(options: nil)
    let result = filter.valueForKey(kCIOutputImageKey) as! CIImage!
    let cgImage = ciContext.createCGImage(result, fromRect: view.frame)

    let finalImage = UIImage(CGImage: cgImage)


    let blurImageView = UIImageView(frame: view.frame)
    blurImageView.image = finalImage
    blurImageView.sizeToFit()
    blurImageView.contentMode = .ScaleAspectFit
    blurImageView.center = view.center
    view.addSubview(blurImageView)

这是我看到的:

除了边缘,它看起来几乎是正确的。似乎模糊度正在从模糊半径到边缘起飞。我尝试使用上下文大小,但似乎无法正常工作。

如何使模糊一直延伸到边缘?

这是因为高斯模糊滤镜对图像边缘以外的像素进行了采样。但是因为没有像素,你会得到这个奇怪的人工制品。您可以使用 "CIAffineClamp" 过滤器在各个方向无限地 "extend" 您的图像。

请看这个回答

我尝试了 运行 你的代码和链式 'CIAffineClamp-> CIGaussianBlur' 过滤器并得到了很好的结果。

let layer = UIApplication.sharedApplication().keyWindow?.layer
UIGraphicsBeginImageContext(view.frame.size)

layer!.renderInContext(UIGraphicsGetCurrentContext()!)
let screenshot = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()

let blurRadius = 5
let ciimage: CIImage = CIImage(image: screenshot)!

// Added "CIAffineClamp" filter 
let affineClampFilter = CIFilter(name: "CIAffineClamp")!
affineClampFilter.setDefaults()
affineClampFilter.setValue(ciimage, forKey: kCIInputImageKey)
let resultClamp = affineClampFilter.valueForKey(kCIOutputImageKey)

// resultClamp is used as input for "CIGaussianBlur" filter
let filter: CIFilter = CIFilter(name:"CIGaussianBlur")!
filter.setDefaults()
filter.setValue(resultClamp, forKey: kCIInputImageKey)
filter.setValue(blurRadius, forKey: kCIInputRadiusKey)


let ciContext = CIContext(options: nil)
let result = filter.valueForKey(kCIOutputImageKey) as! CIImage!
let cgImage = ciContext.createCGImage(result, fromRect: ciimage.extent) // changed to ciiimage.extend 

let finalImage = UIImage(CGImage: cgImage)

let blurImageView = UIImageView(frame: view.frame)
blurImageView.image = finalImage
blurImageView.sizeToFit()
blurImageView.contentMode = .ScaleAspectFit
blurImageView.center = view.center
view.addSubview(blurImageView)