本帖最后由 电音之王 于 2016-10-16 00:38 编辑

Swift 3.0 高斯模糊(图片),没什么好介绍的,直接看效果。

Swift 3.0实现的对图片进行高斯模糊效果,贴出源代码

Swift 3.0实现的对图片进行高斯模糊效果,贴出源代码 - 敏捷大拇指 - Swift 3.0实现的对图片进行高斯模糊效果,贴出源代码


代码如下,注册了Swift开发者社区敏捷大拇指Swifthumb.com)之后可以直接拷贝拿去用。

[Swift] 纯文本查看 复制代码
//MARK: UImage

extension UIImage{

        //增加模糊的效果(需要添加Accelerate.Framework)

        func gaosiBlur(blur:Double) ->UIImage{

                var blurAmount = blur

                //高斯模糊参数(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 = img!.dataProvider

                let inBitmapData = inProvider!.data

                inBuffer.width = vImagePixelCount(img!.width)

                inBuffer.height = vImagePixelCount(img!.height)

                inBuffer.rowBytes = img!.bytesPerRow

                inBuffer.data = UnsafeMutableRawPointer(mutating:CFDataGetBytePtr(inBitmapData))

                //手动申请内存

                let pixelBuffer = malloc(img!.bytesPerRow* img!.height)

                outBuffer.width = vImagePixelCount(img!.width)

                outBuffer.height = vImagePixelCount(img!.height)

                outBuffer.rowBytes = img!.bytesPerRow

                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 = CGContext(data: outBuffer.data,

                                width:Int(outBuffer.width),

                                height:Int(outBuffer.height),

                                bitsPerComponent:8,

                                bytesPerRow: outBuffer.rowBytes,

                                space: colorSpace,

                                bitmapInfo:CGImageAlphaInfo.premultipliedLast.rawValue)

                let imageRef = ctx!.makeImage()

                //手动申请内存

                free(pixelBuffer)

                returnUIImage(cgImage: imageRef!)

        }

}