2009-08-29 49 views
0

如何在位图上下文中对每个像素进行迭代?我发现一些似乎试图做到这一点的来源,但它有点不对。有人能告诉我如何解决这个代码,以便我可以遍历每个像素的RGBA?如何在位图上下文中对每个像素的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; 

}

回答

2

与CGBitmapContextCreate,提供缓冲数据,那么你的像素将是它。
你可以检查这link

+0

使用你的缓冲像素是RGBA所以1像素是4字节长,所以与指针缓冲区,你可以走它。 – CiNN 2009-08-29 14:00:57

+0

请教我如何根据上面给出的代码来做到这一点? – RexOnRoids 2009-08-30 03:37:41

+0

你的新代码似乎有诀窍,用pixelBytes更改字节并将其转换为无符号字符。如果你想访问一个特定的像素有这个公式(y * width)+ x – CiNN 2009-08-30 10:10:01

相关问题