Skip to content

Instantly share code, notes, and snippets.

@alexandrius
Created February 6, 2015 11:55
Show Gist options
  • Save alexandrius/5e17925c809bc1eb2119 to your computer and use it in GitHub Desktop.
Save alexandrius/5e17925c809bc1eb2119 to your computer and use it in GitHub Desktop.
Blur UIImage iOS Swift
func imageWithGaussianBlur(image: UIImage) -> UIImage{
var weight:[CGFloat] = [0.1870270270, 0.2345945946, 0.1816216216, 0.0940540541, 0.0762162162, 0.0940540541, 0.16162162162, 0.1962162162, 0.2040540541, 0.0662162162]
UIGraphicsBeginImageContextWithOptions(image.size, Bool(false), image.scale)
image.drawInRect(CGRectMake(0, 0, image.size.width, image.size.height), blendMode: kCGBlendModeNormal, alpha: weight[0])
var blurCount = Int(blurAmount)
if(blurCount > 9){
blurCount = 9
}
for index in 1...blurCount {
image.drawInRect(CGRectMake(CGFloat(index), 0, image.size.width, image.size.height), blendMode: kCGBlendModeNormal, alpha: weight[index])
image.drawInRect(CGRectMake(CGFloat(-index), 0, image.size.width, image.size.height),blendMode: kCGBlendModeNormal, alpha: weight[index])
}
var horizBlurredImage = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
UIGraphicsBeginImageContextWithOptions(image.size, Bool(false), image.scale)
horizBlurredImage.drawInRect(CGRectMake(0, 0, image.size.width, image.size.height), blendMode:kCGBlendModeNormal, alpha:weight[0] )
for index in 1...blurCount {
horizBlurredImage.drawInRect(CGRectMake(0, CGFloat(index), image.size.width, image.size.height), blendMode:kCGBlendModeNormal, alpha:weight[index])
horizBlurredImage.drawInRect(CGRectMake(0, CGFloat(-index), image.size.width, image.size.height), blendMode:kCGBlendModeNormal, alpha:weight[index])
}
var blurredImage = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
return blurredImage
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment