替换 CGBitmapContext 中的特定颜色
Posted
技术标签:
【中文标题】替换 CGBitmapContext 中的特定颜色【英文标题】:Replace specific color in CGBitmapContext 【发布时间】:2012-08-24 04:18:31 【问题描述】:如何替换 CGBitmapContext
中已经绘制的特定颜色(RGB 值)?
有什么简单的方法吗?
提前致谢。
【问题讨论】:
定义“特定颜色”。您的意思是确切的 RGB 值,例如 (15, 137, 219)?还是您的意思是通俗的“绿色”或“黄色”之类的? 【参考方案1】:您需要通过执行以下操作来获得指向像素的指针和有关其格式的信息:
// This assumes the data is RGBA format, 8-bits per channel.
// You'll need to verify that by calling CGBitmapContextGetBitsPerPixel (), etc.
typedef struct RGBA8
UInt8 red;
UInt8 green;
UInt8 blue;
UInt8 alpha;
RGBA8;
RGBA8* pixels = CGBitmapContextGetData (context);
UInt32 height = CGBitmapContextGetHeight (context);
UInt32 width = CGBitmapContextGetWidth (context);
UInt32 rowBytes = CGBitmapContextGetBytesPerRow (context);
UInt32 x, y;
for (y = 0; y < height; y++)
RGBA8* currentRow = (RGBA8*)((UInt8*)pixels + y * rowBytes);
for (x = 0; x < width; x++)
if ((currentRow->red == replaceRed) && (currentRow->green == replaceGreen) &&
(currentRow->blue == replaceBlue) && (currentRow->alpha == replaceAlpha))
currentRow->red = newRed;
currentRow->green = newGreen;
currentRow->blue = newBlue;
currentRow->alpha = newAlpha;
currentRow++;
【讨论】:
将每个通道乘以 255。所以它是 (R,G,B,A) = (222, 138, 103, 255)。以上是关于替换 CGBitmapContext 中的特定颜色的主要内容,如果未能解决你的问题,请参考以下文章