改变像素的颜色

时间:2009-07-13 11:08:18

标签: iphone

我能够在给定的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 个答案:

答案 0 :(得分: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); 

您应该立即对数据*进行所有更改,然后将修改后的位图缓冲区写回原始上下文。

相关问题