如何在Swift的动态摄影机视图上方添加具有自定义形状的模糊蒙版?

问题描述 投票:5回答:2

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

我已经尝试了几种方法,但是没有一种方法没有覆盖孔就增加了模糊效果。我也花了很多时间在Google上也找不到有效的解决方案。

任何人都可以提供一些关于如何仅使附加到图像视图的png图像的非透明部分模糊的提示吗?]] >>

我尝试过的方法:

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

let blurEffect = UIBlurEffect(style: UIBlurEffectStyle.Dark)
let blurEffectView = UIVisualEffectView(effect: blurEffect)
maskImage.addSubview(blurEffectView)
  • 使用“ 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
    
  • 我想要的视觉效果:

    enter image description here

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

    我已经通过创建一个带有上述模糊图像的图层的叠加层,然后使用从路径绕过扩展区构建的蒙版,然后跳转到抠像孔,向相反方向移动来完成此操作绕组填充规则的方向。

    请参阅此处的文档资料:

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

    尽管有许多已记录的示例,但这是一个:drawing with clear color on UIView (cutting a hole) in static method

    这个问题问了很长时间,但是我看不到任何有用的答案,这就是为什么我要回答它也许对别人有帮助的原因。

    将模糊应用于图像中某些圆形区域的最简单方法,我们可以应用高斯模糊和渐变蒙版。 Here苹果上面贴了一个帖子。

    执行此操作的基本常用步骤:

    1. 使用CIRadialGradient创建遮罩滤镜(在此处调整圆心)

      guard let radialMask = CIFilter(name:"CIRadialGradient") else {
         return nil
      }
      
      let h = inputImage.extent.size.height
      let w = inputImage.extent.size.width
      
      // Adjust your circular hole position here
      let imageCenter = CIVector(x:0.55 * w, y:0.6 * h)
      radialMask.setValue(imageCenter, forKey:kCIInputCenterKey)
      // Small fraction of the image's dimension (high sharp)
      radialMask.setValue(0.2 * h, forKey:"inputRadius0")
      // Large fraction of the image's dimension (lower sharp)
      radialMask.setValue(0.3 * h, forKey:"inputRadius1")
      radialMask.setValue(CIColor(red:0, green:1, blue:0, alpha:0), 
                          forKey:"inputColor0")
      radialMask.setValue(CIColor(red:0, green:1, blue:0, alpha:1), 
                          forKey:"inputColor1")
      
    2. 创建CIMaskedVariableBlur来遮盖模糊区域

      guard let maskedVariableBlur = CIFilter(name:"CIMaskedVariableBlur") else {
         return nil
      }
      // inputImage:- Your original image
      maskedVariableBlur.setValue(inputImage, forKey: kCIInputImageKey)
      // inputRadiusKey:- Right degree of blur desired
      maskedVariableBlur.setValue(10, forKey: kCIInputRadiusKey)
      // here we will use result of radialMask filter result image
      maskedVariableBlur.setValue(radialMask.outputImage, forKey: "inputMask")
      // Get result image
      let selectivelyFocusedCIImage = maskedVariableBlur.outputImage
      // Convert your result image to UIImage
      let resultImage = UIImage(ciImage: selectivelyFocusedCIImage)
      
    3. 额外:-您可以使用UIPanGestureRecognizer移动圆孔

      1-创建手势识别器:

      lazy var panGesture: UIPanGestureRecognizer = {
          let panGesture = UIPanGestureRecognizer(target: self, action: #selector(handleDrag))
          return panGesture
      }()
      

    2-将手势识别器添加到图像视图中

    self.imageView.isUserInteractionEnabled = true
    self.imageView.addGestureRecognizer(panGesture)
    

    3-处理手势

    var initialCenter = CGPoint()
    @objc fileprivate func handleDrag(_ gestureRecognizer: UIPanGestureRecognizer) {
      guard gestureRecognizer.view != nil else {return}
    
      let translation = gestureRecognizer.translation(in: imageView.superview)
    
      if gestureRecognizer.state == .began {
          // Save the view's original position.
          self.initialCenter = CGPoint(x: centerVector.x, y: centerVector.y)
      }
      // Update the position for the .began, .changed, and .ended states
      if gestureRecognizer.state != .cancelled {
          // Add the X and Y translation to the view's original position.
          let newCenter = CGPoint(x: initialCenter.x + translation.x, y: initialCenter.y + -translation.y)
    
    
          self.imageCenter = CIVector.init(cgPoint: newCenter)
          // Then apply your filter on gestureRecognizer.state == .end
    
      } else {
          // On cancellation, return the piece to its original location.
          self.imageCenter = CIVector(x: initialCenter.x, y: initialCenter.y)
      }
    }
    

    快乐的编码...

    ios swift camera blur
    2个回答
    2
    投票

    我已经通过创建一个带有上述模糊图像的图层的叠加层,然后使用从路径绕过扩展区构建的蒙版,然后跳转到抠像孔,向相反方向移动来完成此操作绕组填充规则的方向。


    0
    投票

    这个问题问了很长时间,但是我看不到任何有用的答案,这就是为什么我要回答它也许对别人有帮助的原因。

    将模糊应用于图像中某些圆形区域的最简单方法,我们可以应用高斯模糊和渐变蒙版。 Here苹果上面贴了一个帖子。

    © www.soinside.com 2019 - 2024. All rights reserved.