如何在位图上下文中迭代EACH像素的RGBA?

时间:2009-08-29 09:41:50

标签: iphone uikit uiimage core-graphics cgimage

如何在位图上下文中迭代EACH PIXEL?我找到了一些似乎试图这样做的消息来源,但它有点瑕疵。有人能告诉我如何修复这段代码,以便我可以迭代每个像素的RGBA吗?

-(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; 

}

1 个答案:

答案 0 :(得分:2)

使用CGBitmapContextCreate,为数据提供缓冲区,然后您的像素将在其中 您可以查看此link

相关问题