【问题标题】:How can I iterate on RGBA of EACH pixel in a bitmap context?如何在位图上下文中迭代每个像素的 RGBA?
【发布时间】:2009-08-29 09:41:49
【问题描述】:

如何在位图上下文中迭代每个像素?我发现了一些似乎试图这样做的来源,但它存在某种缺陷。谁能告诉我如何修复此代码,以便我可以迭代每个像素的 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; 

}

【问题讨论】:

    标签: iphone uikit uiimage core-graphics cgimage


    【解决方案1】:

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

    【讨论】:

    • 使用缓冲像素是 RGBA,因此 1 个像素的长度为 4 个字节,因此使用指向缓冲区的指针可以遍历它。
    • 您能根据上面给出的代码教我如何做到这一点吗?
    • 您的新代码似乎可以解决问题,使用 pixelBytes 更改字节并将其转换为无符号字符。如果你想访问一个特定的像素有这个公式 (y * width) + x
    • CiNN-我如何使用这个公式:-( y * width ) + x 你能提供一个样本来修改位图图像的像素
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-04-19
    • 1970-01-01
    • 2018-01-05
    相关资源
    最近更新 更多