本文介绍了如何在Swift中的动态相机视图上方添加自定义形状的模糊蒙版?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Swift开发具有相机功能的iOS应用程序,它需要在相机视图上方有一个模糊图层,中间有一个孔,如下图所示。



我尝试了几种方法,但没有一种方法在没有覆盖孔的情况下添加了模糊效果。我也没有在谷歌上花时间后找到合适的解决方案。



任何人都可以提供一些提示,我怎么才能模糊非透明部分附加到图像视图的png图像?



我尝试过的方法:


  1. 使用内置iOS 8模糊效果

     让blurEffect = UIBlurEffect(样式:UIBlurEffectStyle.Dark)
    let blurEffectView = UIVisualEffectView(effect:blurEffect)
    maskImage.addSubview(blurEffectView)


  2. 使用CIGaussianBlur过滤器

      var imageToBlur = CIImage(image:image)
    var blurfilter = CIFilter(name:CIGaussianBlur)
    blurfilter.setValue(imageToBlur,forKey:inputImage)
    var resultImage = blurfilter.valueForKey(outputImage)as! CIImage
    var blurredImage = UIImage(CIImage:resultImage)
    self.maskImage.image = blurredImage


我想要的视觉效果:





虽然有很多记录在案的例子,但这里有一个:
以静态方法在UIView(切洞)上绘制清晰的颜色


I am using Swift to develop an iOS application with a camera feature, and it requires a blur layer above the camera view with a hole in the middle like the image shown below.

I have tried several methods, but none of them added the blur effect without covering the hole. I didn't find working solutions after spending time on Google as well.

Can anyone provide some hints on how can I only blur the non-transperant part of a png image that is attached to a image view?

Methods that I have tried:

  1. Use the built-in iOS 8 blur effect

    let blurEffect = UIBlurEffect(style: UIBlurEffectStyle.Dark)
    let blurEffectView = UIVisualEffectView(effect: blurEffect)
    maskImage.addSubview(blurEffectView)
    

  2. Use the "CIGaussianBlur" Filter

    var imageToBlur = CIImage(image: image)
    var blurfilter = CIFilter(name: "CIGaussianBlur")
    blurfilter.setValue(imageToBlur, forKey: "inputImage")
    var resultImage = blurfilter.valueForKey("outputImage") as! CIImage
    var blurredImage = UIImage(CIImage: resultImage)
    self.maskImage.image = blurredImage
    

The visual effect that I want to have:

解决方案

I've done this by creating an overlay with a layer with said blurred image, and then using a mask that is built from a path that goes all the way around the extents, and then jumps to the cutout hole, going the opposite direction with the winding fill rule.

Refer to documetation here:

https://developer.apple.com/library/ios/documentation/GraphicsImaging/Conceptual/drawingwithquartz2d/dq_paths/dq_paths.html

There are many documented examples of this though, here is one:drawing with clear color on UIView (cutting a hole) in static method

这篇关于如何在Swift中的动态相机视图上方添加自定义形状的模糊蒙版?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-09 17:49