Skip to content

Instantly share code, notes, and snippets.

@maojj
Created June 14, 2013 08:28
Show Gist options
  • Save maojj/5780351 to your computer and use it in GitHub Desktop.
Save maojj/5780351 to your computer and use it in GitHub Desktop.
-(UIImage*)getGrayImage:(UIImage*)sourceImage
{
int width = sourceImage.size.width;
int height = sourceImage.size.height;
CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceGray();
CGContextRef context = CGBitmapContextCreate (nil,width,height,8,0,colorSpace,kCGImageAlphaNone);
CGColorSpaceRelease(colorSpace);
if (context == NULL) {
return nil;
}
CGContextDrawImage(context,CGRectMake(0, 0, width, height), sourceImage.CGImage);
UIImage *grayImage = [UIImage imageWithCGImage:CGBitmapContextCreateImage(context)];
CGContextRelease(context);
return grayImage;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment