zoukankan      html  css  js  c++  java
  • iOS-实现高斯模糊效果(swift)

    UIImage添加分类

    extension UIImage {
        /// 高斯模糊
        func gaussianBlur(var blurAmount:CGFloat) -> UIImage {
            //高斯模糊参数(0-1)之间,超出范围强行转成0.5
            if (blurAmount < 0.0 || blurAmount > 1.0) {
                blurAmount = 0.5
            }
            
            var boxSize = Int(blurAmount * 40)
            boxSize = boxSize - (boxSize % 2) + 1
            
            let img = self.CGImage
            
            var inBuffer = vImage_Buffer()
            var outBuffer = vImage_Buffer()
            
            let inProvider =  CGImageGetDataProvider(img)
            let inBitmapData =  CGDataProviderCopyData(inProvider)
            
            inBuffer.width = vImagePixelCount(CGImageGetWidth(img))
            inBuffer.height = vImagePixelCount(CGImageGetHeight(img))
            inBuffer.rowBytes = CGImageGetBytesPerRow(img)
            inBuffer.data = UnsafeMutablePointer<Void>(CFDataGetBytePtr(inBitmapData))
            
            //手动申请内存
            let pixelBuffer = malloc(CGImageGetBytesPerRow(img) * CGImageGetHeight(img))
            
            outBuffer.width = vImagePixelCount(CGImageGetWidth(img))
            outBuffer.height = vImagePixelCount(CGImageGetHeight(img))
            outBuffer.rowBytes = CGImageGetBytesPerRow(img)
            outBuffer.data = pixelBuffer
            
            var error = vImageBoxConvolve_ARGB8888(&inBuffer,
                &outBuffer, nil, vImagePixelCount(0), vImagePixelCount(0),
                UInt32(boxSize), UInt32(boxSize), nil, vImage_Flags(kvImageEdgeExtend))
            if (kvImageNoError != error)
            {
                error = vImageBoxConvolve_ARGB8888(&inBuffer,
                    &outBuffer, nil, vImagePixelCount(0), vImagePixelCount(0),
                    UInt32(boxSize), UInt32(boxSize), nil, vImage_Flags(kvImageEdgeExtend))
                if (kvImageNoError != error)
                {
                    error = vImageBoxConvolve_ARGB8888(&inBuffer,
                        &outBuffer, nil, vImagePixelCount(0), vImagePixelCount(0),
                        UInt32(boxSize), UInt32(boxSize), nil, vImage_Flags(kvImageEdgeExtend))
                }
            }
            
            let colorSpace =  CGColorSpaceCreateDeviceRGB()
            let ctx = CGBitmapContextCreate(outBuffer.data,
                Int(outBuffer.width),
                Int(outBuffer.height),
                8,
                outBuffer.rowBytes,
                colorSpace,
                CGImageAlphaInfo.PremultipliedLast.rawValue)
            
            let imageRef = CGBitmapContextCreateImage(ctx)
            
            //手动申请内存
            free(pixelBuffer)
            return UIImage(CGImage: imageRef!)
        }
    }
    
  • 相关阅读:
    10进制转换为二十六进制字符串A-Z
    解决Missing artifact jdk.tools:jdk.tools:jar:1.8报错
    JAVA中AES对称加密和解密以及与Python兼容
    Nginx配置客户端SSL双向认证
    (备忘)最全的正则表达式
    (备忘)Python字符串、元组、列表、字典互相转换的方法
    (备忘)Nodepad++常用快捷键
    (备忘)正则表达式全部符号解释
    (备忘)Window7下安装Python2.6及Django1.4
    (备忘)Java Map 遍历
  • 原文地址:https://www.cnblogs.com/lancely/p/5782781.html
Copyright © 2011-2022 走看看