Skip to content

Instantly share code, notes, and snippets.

@nevyn
Created May 15, 2010 19:12
Show Gist options
  • Save nevyn/402356 to your computer and use it in GitHub Desktop.
Save nevyn/402356 to your computer and use it in GitHub Desktop.
Get the pixels of an UIImage
@implementation UIImage (BCGetPixels)
-(void)bc_getPixels:(char*)pixels;
{
CGImageRef imageRef = self.CGImage;
NSUInteger width = CGImageGetWidth(imageRef);
NSUInteger height = CGImageGetHeight(imageRef);
CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceGray();
NSUInteger bytesPerPixel = 1;
NSUInteger bytesPerRow = bytesPerPixel * width;
NSUInteger bitsPerComponent = 8;
CGContextRef context = CGBitmapContextCreate(
pixels,
width, height,
bitsPerComponent,
bytesPerRow,
colorSpace,
kCGImageAlphaNone
);
CGColorSpaceRelease(colorSpace);
CGContextDrawImage(context, CGRectMake(0, 0, width, height), imageRef);
CGContextRelease(context);
}
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment