Skip to content

Instantly share code, notes, and snippets.

@hhyyy9
Created September 29, 2012 01:26
Show Gist options
  • Save hhyyy9/3802846 to your computer and use it in GitHub Desktop.
Save hhyyy9/3802846 to your computer and use it in GitHub Desktop.
返回图像的灰度模式
+ (UIImage *) grayscaleImage: (UIImage *) image
{
CGSize size = image.size;
CGRect rect = CGRectMake(0.0f, 0.0f, image.size.width,
image.size.height);
// Create a mono/gray color space
CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceGray();
CGContextRef context = CGBitmapContextCreate(nil, size.width,
size.height, 8, 0, colorSpace, kCGImageAlphaNone);
CGColorSpaceRelease(colorSpace);
// Draw the image into the grayscale context
CGContextDrawImage(context, rect, [image CGImage]);
CGImageRef grayscale = CGBitmapContextCreateImage(context);
CGContextRelease(context);
// Recover the image
UIImage *img = [UIImage imageWithCGImage:grayscale];
CFRelease(grayscale);
return img;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment