Skip to content

Instantly share code, notes, and snippets.

@quangtqag
Created May 24, 2016 07:40
Show Gist options
  • Select an option

  • Save quangtqag/d00f9ddf3cb8d2b298bf5c5c0108ae16 to your computer and use it in GitHub Desktop.

Select an option

Save quangtqag/d00f9ddf3cb8d2b298bf5c5c0108ae16 to your computer and use it in GitHub Desktop.
func grayScale() -> UIImage {
let width = self.size.width
let height = self.size.height
let imageRect:CGRect = CGRectMake(0, 0, width, height)
let colorSpace = CGColorSpaceCreateDeviceGray()
let bitmapInfo = CGBitmapInfo(rawValue: CGImageAlphaInfo.None.rawValue)
let context = CGBitmapContextCreate(nil, Int(width), Int(height), 8, 0, colorSpace, bitmapInfo.rawValue)
CGContextDrawImage(context, imageRect, self.CGImage)
let imageRef = CGBitmapContextCreateImage(context)
let grayScaleImage = UIImage(CGImage: imageRef!)
return grayScaleImage
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment