【问题标题】:UIVisualEffectView blur imageUIVisualEffectView 模糊图像
【发布时间】:2019-02-02 22:03:04
【问题描述】:

我使用 UIVisualEffectView 来模糊我的图像视图。比我使用此代码将其转换为图像并保存到图像库。

 UIGraphicsBeginImageContextWithOptions(imageContainerView.bounds.size, true, 1)
 imageContainerView.drawHierarchy(in: imageContainerView.bounds, afterScreenUpdates: true)
 let newImage = UIGraphicsGetImageFromCurrentImageContext()!
 UIGraphicsEndImageContext()

imageContainerView 是一个包含 imageview 和 UIVisualEffectView 的视图。

在我将 imageContainerView 转换为图像之前,我将它的大小增加到 2000x2000,这样我将获得高质量的图像,如果我不这样做,imageContainerView 中的所有图像都会像素化。

问题是在将 imageContainerView 转换为 image 后,模糊的图像几乎不会模糊,因为我增加了容器视图的大小。

您能为这个问题提出一个解决方案吗? 我只是想得到一个模糊的图像,所以如果你知道使用滑块模糊图像的其他方法(模糊代码应该可以快速运行,因此 UI 不会冻结),请告诉我。

【问题讨论】:

  • 您是否尝试过调整imageContainerView的大小并将UIGraphicsBeginImageContextWithOptionsscale参数设置为0(这将自动匹配设备的比例因子你正在使用)? UIGraphicsBeginImageContextWithOptions(imageContainerView.bounds.size, true, 0)
  • 你能解释一下有什么区别吗?假设设备的比例因子为 2,因此它将使 containerView 的大小翻倍。如果我手动操作有什么区别?

标签: ios swift xcode image-processing blur


【解决方案1】:

我只想得到一个模糊的图像

好的,你可以实现一些模糊滤镜效果,比如这个,然后滑动到它。

var context = CIContext(options: nil)

func blur() {

    let blurFilter = CIFilter(name: "CIGaussianBlur") 
    let beginImage = CIImage(image: YOURIMAGE!)
    blurFilter!.setValue(beginImage, forKey: kCIInputImageKey)
    blurFilter!.setValue(8, forKey: kCIInputRadiusKey)

    let cropFilter = CIFilter(name: "CICrop")
    cropFilter!.setValue(blurFilter!.outputImage, forKey: kCIInputImageKey)
    cropFilter!.setValue(CIVector(cgRect: beginImage!.extent), forKey: "inputRectangle")

    let output = cropFilter!.outputImage 
    let cgImg = context.createCGImage(output!, from: output!.extent)
    let processedImage = UIImage(cgImage: cg cgImg mg!)
    bg.image = processedImage
}

或者 GitHub 上有多个类似这样的库,例如:https://github.com/FlexMonkey/Blurable

【讨论】:

  • 与仅更改视觉效果视图的 alpha 相比,您的解决方案要慢得多
猜你喜欢
  • 2014-07-26
  • 1970-01-01
  • 2014-09-18
  • 1970-01-01
  • 1970-01-01
  • 2018-08-20
  • 2015-06-10
  • 2014-06-13
  • 2015-09-19
相关资源
最近更新 更多