Created
November 18, 2011 22:15
-
-
Save epatel/1377949 to your computer and use it in GitHub Desktop.
Get the RGBA values for a pixel from an image
This file contains 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
CGImageRef image = uiimage.CGImage; | |
NSUInteger width = CGImageGetWidth(image); | |
NSUInteger height = CGImageGetHeight(image); | |
// Setup 1x1 pixel context to draw into | |
CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB(); | |
unsigned char rawData[4]; | |
int bytesPerPixel = 4; | |
int bytesPerRow = bytesPerPixel; | |
NSUInteger bitsPerComponent = 8; | |
CGContextRef context = CGBitmapContextCreate(rawData, | |
1, | |
1, | |
bitsPerComponent, | |
bytesPerRow, | |
colorSpace, | |
kCGImageAlphaPremultipliedLast | kCGBitmapByteOrder32Big); | |
CGColorSpaceRelease(colorSpace); | |
CGContextSetBlendMode(context, kCGBlendModeCopy); | |
// Draw the image | |
CGContextDrawImage(context, | |
CGRectMake(-offset.x, offset.y-height, width, height), | |
image); | |
// Done | |
CGContextRelease(context); | |
// Get the pixel information | |
unsigned char red = rawData[0]; | |
unsigned char green = rawData[1]; | |
unsigned char blue = rawData[2]; | |
unsigned char alpha = rawData[3]; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment