Take the 2-minute tour ×

I'm quite new to working with UIImages on the byte level, but I was hoping that someone could point me to some guides on this matter?

I am ultimately looking to edit the RGBA values of the bytes, based on certain parameters (position, color, etc.) and I know I've come across samples/tutorials for this before, but I just can't seem to find anything now.

Basically, I'm hoping to be able to break a UIImage down to its bytes and iterate over them and edit the bytes' RGBA values individually. Maybe some sample code here would be a big help as well.

I've already been working in the different image contexts and editing the images with the CG power tools, but I would like to be able to work at the byte level.


Sorry, but I do understand that you cannot edit the bytes in a UIImage directly. I should have asked my question more clearly. I meant to ask how can I get the bytes of a UIImage, edit those bytes and then create a new UIImage from those bytes.

As pointed out by @BradLarson, OpenGL is a better option for this and there is a great library, which was created by @BradLarson, here. Thanks @CSmith for pointing it out!

share|improve this question
What specifically do you want to do with the image bytes? Run an operation on each pixel? If so, you might want to look at using OpenGL ES fragment shaders instead of a slower iteration over the pixels on the CPU. An OpenGL ES implementation can be up to 100X faster than a CPU-bound loop. –  Brad Larson Aug 23 '12 at 19:58
@BradLarson has awesome GPUImage library you can download to learn OpenGL ES programming, worked for me! –  CSmith Aug 23 '12 at 20:04
@BradLarson Thanks so much! I will definitely go through your GPUImage open source library that CSmith pointed out. Thanks for the help! –  RileyE Aug 23 '12 at 20:58
add comment

2 Answers

up vote 1 down vote accepted

@MartinR has right answer, here is some code to get you started:

UIImage *image = your image;

CGImageRef imageRef = image.CGImage;
NSUInteger nWidth = CGImageGetWidth(imageRef);
NSUInteger nHeight = CGImageGetHeight(imageRef);
NSUInteger nBytesPerRow = CGImageGetBytesPerRow(imageRef);
NSUInteger nBitsPerPixel = CGImageGetBitsPerPixel(imageRef);
NSUInteger nBitsPerComponent = CGImageGetBitsPerComponent(imageRef);
NSUInteger nBytesPerPixel = nBitsPerPixel == 24 ? 3 : 4;

unsigned char *rawInput = malloc (nWidth * nHeight * nBytesPerPixel);

CGColorSpaceRef colorSpaceRGB = CGColorSpaceCreateDeviceRGB();
CGContextRef context = CGBitmapContextCreate(rawInput, nWidth, nHeight, nBitsPerComponent, nBytesPerRow, colorSpaceRGB, kCGImageAlphaNoneSkipFirst | kCGBitmapByteOrder32Big);
CGContextDrawImage (context, CGRectMake(0, 0, nWidth, nHeight), imageRef);          

// modify the pixels stored in the array of 4-byte pixels at rawInput

UIImage *imageNew = [[UIImage alloc] initWithCGImage:CGBitmapContextCreateImage(context)];

CGContextRelease (context);
free (rawInput);
share|improve this answer
To modify a specific pixel's RGBA contents, I would access them as such: R => rawInput[i*nBitsPerPixel], G => rawInput[i*nBitsPerPixel + nBitsPerComponent], B => rawInput[i*nBitsPerPixel + nBitsPerComponent*2], A => rawInput[i*nBitsPerPixel + nBitsPerComponent*3], where i is the position in the iteration? And can I set the values (I'm assuming they're from 0.0f - 1.0f) directly? –  RileyE Aug 23 '12 at 20:57
each pixel is four bytes, RGBA (e.g. nBytesPerPixel == 4). They are bytes, not floats. Row 0, column 0 is first four bytes, row 0 column 1 is next four bytes, etc. While this will get you going, OpenGL is worth the effort, especially if your bitmaps are large. –  CSmith Aug 24 '12 at 12:22
To set the first pixel's blue, I would essentially access the third position in the byte array and I could set it to 0xff for 255 blue? –  RileyE Aug 24 '12 at 13:23
Yes, thats correct –  CSmith Aug 24 '12 at 16:44
add comment

You have no direct access to the bytes in an UIImage and you cannot change them directly.

You have to draw the image into a CGBitmapContext, modify the pixels in the bitmap, and then create a new image from the bitmap context.

share|improve this answer
add comment

Your Answer


