Created
October 10, 2012 06:50
-
-
Save jlcampana/3863564 to your computer and use it in GitHub Desktop.
Imagen a Grayscale
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
+ (UIImage *)convertImageToGrayScale:(UIImage *)img | |
{ | |
// Create image rectangle with current image width/height | |
CGRect imageRect = CGRectMake(0, 0, img.size.width, img.size.height); | |
// Grayscale color space | |
CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceGray(); | |
// Create bitmap content with current image size and grayscale colorspace | |
CGContextRef context = CGBitmapContextCreate(nil, img.size.width, img.size.height, 8, 0, colorSpace, kCGImageAlphaNone); | |
// Draw image into current context, with specified rectangle | |
// using previously defined context (with grayscale colorspace) | |
CGContextDrawImage(context, imageRect, [img CGImage]); | |
// Create bitmap image info from pixel data in current context | |
CGImageRef imageRef = CGBitmapContextCreateImage(context); | |
// Create a new UIImage object | |
UIImage *newImage = [UIImage imageWithCGImage:imageRef]; | |
// Release colorspace, context and bitmap information | |
CGColorSpaceRelease(colorSpace); | |
CGContextRelease(context); | |
if (imageRef) | |
CFRelease(imageRef); | |
// Return the new grayscale image | |
return newImage; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment