【问题标题】:How to add a blur mask with a custom shape above a dynamic camera view in Swift?如何在 Swift 中的动态相机视图上方添加具有自定义形状的模糊蒙版?
【发布时间】:2015-07-27 20:43:37
【问题描述】:

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

我尝试了几种方法,但都没有在不覆盖孔的情况下添加模糊效果。在 Google 上花费时间后,我也没有找到可行的解决方案。

谁能提供一些关于如何仅模糊附加到图像视图的 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
    

我想要的视觉效果:

【问题讨论】:

  • 顺便说一下,当前图层是png格式的图片,中间有个洞,贴在相机视图上方的一个图片视图上。

标签: ios swift camera blur


【解决方案1】:

我通过创建一个带有所述模糊图像的图层的叠加层来做到这一点,然后使用一个蒙版,该蒙版是从一条一直围绕范围的路径构建的,然后跳到切口孔,去与缠绕填充规则方向相反。

请参阅此处的文档:

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

【讨论】:

  • 抱歉我的回复晚了。您能否提供部分代码您是如何解决的?阅读链接后,我不知道如何从 UIImageView 中切出洞。谢谢!
  • 一些示例代码或更清晰的解释真的会帮助这个答案更有用。将不胜感激。
【解决方案2】:

在图像中的某个圆形区域外应用模糊的最简单方法是应用高斯模糊和渐变蒙版。 HereApple 上面有帖子。

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

  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)
  }
}

【讨论】:

    猜你喜欢
    • 2023-03-19
    • 2021-09-18
    • 1970-01-01
    • 2015-03-05
    • 1970-01-01
    • 2015-12-05
    • 2011-11-30
    • 1970-01-01
    • 2023-03-22
    相关资源
    最近更新 更多