Created
July 15, 2015 19:03
-
-
Save wess/6f09b2a08eebf29f2a99 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
extension CGImage { | |
func blurEffect(boxSize: Float) -> CGImageRef! { | |
let boxSize = boxSize - (boxSize % 2) + 1 | |
let inProvider = CGImageGetDataProvider(self) | |
let height = vImagePixelCount(CGImageGetHeight(self)) | |
let width = vImagePixelCount(CGImageGetWidth(self)) | |
let rowBytes = CGImageGetBytesPerRow(self) | |
let inBitmapData = CGDataProviderCopyData(inProvider) | |
let inData = UnsafeMutablePointer<Void>(CFDataGetBytePtr(inBitmapData)) | |
var inBuffer = vImage_Buffer(data: inData, height: height, width: width, rowBytes: rowBytes) | |
let outData = malloc(CGImageGetBytesPerRow(self) * CGImageGetHeight(self)) | |
var outBuffer = vImage_Buffer(data: outData, height: height, width: width, rowBytes: rowBytes) | |
let error = vImageBoxConvolve_ARGB8888(&inBuffer, &outBuffer, nil, 0, 0, UInt32(boxSize), UInt32(boxSize), nil, vImage_Flags(kvImageEdgeExtend)) | |
var colorSpace = CGColorSpaceCreateDeviceRGB() | |
let context = CGBitmapContextCreate(outBuffer.data, Int(outBuffer.width), Int(outBuffer.height), 8, outBuffer.rowBytes, colorSpace, CGImageGetBitmapInfo(self)) | |
let imageRef = CGBitmapContextCreateImage(context) | |
free(outData) | |
return imageRef | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment