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

时间:2015-07-27 20:43:37

标签: ios swift camera blur

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

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

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

我尝试的方法:

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

    let blurEffect = UIBlurEffect(style: 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
    
  3. 我想要的视觉效果:

    enter image description here

2 个答案:

答案 0 :(得分:2)

我已经通过创建一个带有所述模糊图像的图层的叠加,然后使用从一个遍历范围的路径构建的蒙版,然后跳转到切口孔,与缠绕填充规则相反的方向。

请参阅此处的文件:

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

答案 1 :(得分:1)

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

将模糊应用于图像中某些圆形区域的最简单方法是,我们可以应用高斯模糊和渐变蒙版。 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)
    

其他:-您可以使用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)
  }
}

快乐的编码...