Question

How do I iterate on EACH PIXEL in a bitmap context? I found some source that seems to be attempting to do this but it is somehow flawed. Can someone tell me how to fix this code so that I can iterate on the RGBA of each pixel?

-(UIImage*)modifyPixels:(UIImage*)originalImage
{

NSData* pixelData = (NSData*)CGDataProviderCopyData(CGImageGetDataProvider(originalImage.CGImage));
void* pixelBytes = [pixelData bytes];

// Take away the red pixel, assuming 32-bit RGBA
for(int i = 0; i < [pixelData length]; i += 4) {

      bytes[i] = 0; // red
      bytes[i+1] = bytes[i+1]; // green
      bytes[i+2] = bytes[i+2]; // blue
      bytes[i+3] = bytes[i+3]; // alpha
    }

    NSData* newPixelData = [NSData dataWithBytes:pixelBytes length:[pixelData length]];
    UIImage* newImage = [UIImage imageWithData:newPixelData]; 

    return newImage; 

}

Was it helpful?

Solution

with CGBitmapContextCreate, provide a buffer for data, then your pixels will be in it.
You can check this link

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top