2009-07-13 219 views
1

我能够在给定的CGPoint中读取特定的像素,但我一直在寻找更改像素的颜色,如果有人能帮我解决一个代码片段,我将非常感谢。更改像素的颜色

我的代码是:

unsigned char* data = CGBitmapContextGetData (cgctx);  

if (data != NULL) {  
    offset = 4*((w*round(point.y))+round(point.x));  
    alpha = data[offset];  
    red = data[offset+1];    
    green = data[offset+2];   
    blue = data[offset+3];  
    color = [UIColor colorWithRed:(red/255.0f) 
        green:(green/255.0f) blue:(blue/255.0f) 
        alpha:(alpha/255.0f)];  
} 

回答

1

目前还不清楚是什么你正在尝试做的。如果你想改变像素的颜色在原来的CGImageRef,那么你会使用类似:


// Set the color of the pixel to 50% grey + 50% alpha 
data[offset+0] = 128; 
data[offset+1] = 128; 
data[offset+2] = 128; 
data[offset+3] = 128; 

// Create a CGBitmapImageContext 
CGContextRef bitmapContext = CGBitmapContextCreate(data, width, height, CGImageGetBitsPerComponent(), width * 4, CGImageGetColorSpace(), kCGImageAlphPremultipliedFirst); 

// Draw the bitmap context back to your original context 
CGContextDrawImage(bitmapContext, CGMakeRect(...), cgctx); 

你应该把所有的更改数据*一次,然后写入修改后的位图缓冲区回到原来的上下文。

+0

thanx mark, 其实我试图实现洪水填充,但我不能做到这一点,当我尝试匹配像素与它的像素值附近,它给了我虚假。 该场景是我有一个背景图像,并有一些形状,用户可以从调色板中选择颜色,并单击我想尝试洪水填充对象。 我非常需要帮助,因为我无法填充颜色。 – xavoDev 2009-07-14 06:24:32