首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >替换CGBitmapContext中的特定颜色

替换CGBitmapContext中的特定颜色
EN

Stack Overflow用户
提问于 2012-08-24 12:18:31
回答 1查看 520关注 0票数 3

如何替换已经绘制的CGBitmapContext中的特定颜色(RGB值)?

有什么简单的方法吗?

提前谢谢。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2012-08-24 12:58:54

您将希望通过执行以下操作来获得指向像素的指针和有关其格式的信息:

代码语言:javascript
复制
// 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++;
    }
}
票数 4
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/12103083

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档