Switf 实现高斯模糊

Switf 实现高斯模糊

总结下项目中高斯模糊效果中的问题和解决方案

方式一 毛玻璃效果、

通过加一层蒙版来实现,需要注意的是通过这个方式调用截图的时候,模糊蒙版不会被截图上

extension UIVisualEffectView {
    /// 根据模糊程度生成 UIVisualEffectView
    public convenience init(blurRadius: CGFloat) {
        let blurEffect = (NSClassFromString("_UICustomBlurEffect") as! UIBlurEffect.Type).init()
        blurEffect.setValue(blurRadius, forKeyPath: "blurRadius")

        self.init(effect: blurEffect)
    }
}
    let visualEffectView = UIVisualEffectView(blurRadius: 8)
    visualEffectView.frame.size = CGSize(width: Device.width, height: Device.height)
    /// 加到蒙版视图上
     movieBlurImage.addSubview(visualEffectView)
              

方式二 CoreImage、

这种方式是采用滤镜来实现高斯模糊效果,里面有很多KEY 感兴趣的同学可以点进去看看 复古风之类的.....
但是这个方式有个很大的问题就是生成的图片周围有白边,分析的原因可能是位图和矢量渲染引起的,貌似无法解决

  /// 滤镜来实现模糊效果
    /// - Parameters:
    ///   - _movieBlurImage: 传入要高斯的图片
    ///   - blurRadius: 模糊值
    func getBlueImage(_movieBlurImage:UIImage?,blurRadius:CGFloat = 0) -> UIImage? {
        if let blurImage = _movieBlurImage {
            
            let inputImage = CIImage(image: blurImage)
            let filter = CIFilter(name: "CIGaussianBlur")!
            filter.setValue(inputImage, forKey: kCIInputImageKey)
            filter.setValue(blurRadius, forKey: kCIInputRadiusKey)
            let outputCIImage = filter.outputImage!
            /// 需要注意的是这个地方的size  要传 传入图片的size (因为根据位图和矢量的渲染 图片比例会被压缩)
            let cgImage = context.createCGImage(outputCIImage, from: inputImage!.extent)
            return UIImage(cgImage: cgImage!)
            
        }else {
            return nil
        }
    }

方式三 vImage、

我工程里目前是采用这种方式来实现搞死模糊效果的

import Accelerate

private func vBlurImage(_ image: UIImage, blurNumber blur: Float) -> UIImage {
    var blur = blur
    if blur < 0.0 || blur > 1.0 {
        blur = 0.5
    }
    var boxSize = Int(blur * 40)
    boxSize = boxSize - (boxSize % 2) + 1
    
    let img = image.cgImage!
    
    var inBuffer = vImage_Buffer()
    var outBuffer = vImage_Buffer()
    var error: vImage_Error!
    var pixelBuffer: UnsafeMutableRawPointer?
    
    // 从CGImage中获取数据
    let inProvider = img.dataProvider!
    let inBitmapData = inProvider.data!
    
    // 设置从CGImage获取对象的属性
    inBuffer.width = UInt(img.width)
    inBuffer.height = UInt(img.height)
    inBuffer.rowBytes = img.bytesPerRow
    inBuffer.data = UnsafeMutableRawPointer(mutating: CFDataGetBytePtr(inBitmapData))
    pixelBuffer = malloc(img.bytesPerRow * img.height)
    if pixelBuffer == nil {
        print("No pixel buffer!")
    }
    
    outBuffer.data = pixelBuffer
    outBuffer.width = UInt(img.width)
    outBuffer.height = UInt(img.height)
    outBuffer.rowBytes = img.bytesPerRow
    
    error = vImageBoxConvolve_ARGB8888(&inBuffer, &outBuffer, nil, 0, 0, UInt32(boxSize), UInt32(boxSize), nil, UInt32(kvImageEdgeExtend))
    if error != nil && error != 0 {
        print("error from convolution \(error)")
    }
    
    let colorSpace = CGColorSpaceCreateDeviceRGB()
    let ctx = CGContext(data: outBuffer.data, width: Int(outBuffer.width), height: Int(outBuffer.height), bitsPerComponent: 8, bytesPerRow: outBuffer.rowBytes, space: colorSpace, bitmapInfo: CGImageAlphaInfo.noneSkipLast.rawValue)
    
    let imageRef = ctx!.makeImage()!
    let returnImage = UIImage(cgImage: imageRef)
    

    
    return returnImage
}

你可能感兴趣的:(Switf 实现高斯模糊)