i am able to read a particular pixel at given CGPoint but i have been looking to change the color of a pixel and it would be highly appreciated if anyone can help me out with a code snippet.
我能够在给定的CGPoint读取特定像素,但我一直在寻找更改像素的颜色,如果有人可以帮我解决代码片段,我将非常感激。
My Code is:
我的代码是:
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 个解决方案
#1
It's not clear what you are trying to do. If you want to change the pixel's color in the original CGImageRef then you would use something like:
目前尚不清楚你要做什么。如果你想改变原始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);
You should make all of your changes to the data* at once and then write the modified bitmap buffer back to the original context.
您应该立即对数据*进行所有更改,然后将修改后的位图缓冲区写回原始上下文。
#1
It's not clear what you are trying to do. If you want to change the pixel's color in the original CGImageRef then you would use something like:
目前尚不清楚你要做什么。如果你想改变原始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);
You should make all of your changes to the data* at once and then write the modified bitmap buffer back to the original context.
您应该立即对数据*进行所有更改,然后将修改后的位图缓冲区写回原始上下文。