iPhone:如何更改图像的颜色

发布于 2024-08-02 15:40:21 字数 158 浏览 3 评论 0原文

我有一个来自数据库的小图像,该图像的平均颜色需要稍微改变。

这是一个CGImageRef,我想创建一个CGContext,将图像绘制到这个上下文中,然后以某种方式更改位图数据,最后渲染它。 但我怎样才能改变颜色信息呢?

感谢您的帮助!

I have a small image from a database and the image's average color need to be altered slightly.

It's a CGImageRef and I thought of creating a CGContext, drawing the image to this context, then subsequently changing the bitmap data somehow and finally rendering it.
But how can I alter the color information?

Thanks for your help!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

独留℉清风醉 2024-08-09 15:40:21

请查看有关像素数据操作的 Apple 问答,详细了解如何操作可能会这样做。

Check out this Apple Q&A on pixel data manipulation for details on how you might go about that.

拥有 2024-08-09 15:40:21

像这样在对象上绘制颜色:

    // first draw image
    [self.image drawInRect:rect];

    // prepare the context to draw into
    CGContextRef context = UIGraphicsGetCurrentContext();

    // set the blend mode and draw rectangle on top of image
    CGContextSetBlendMode(context, kCGBlendModeColor);
    CGContextClipToMask(context, self.bounds, image.CGImage); // this restricts drawing to within alpha channel
    CGContextSetRGBFillColor(context, 0.75, 0.0, 0.0, 1.0); // this is your color,  a light reddish tint
    CGContextFillRect(context, rect);       

我将其放入自定义 UIView 的 drawRect: 方法中。该 UIView 有一个 ivar,UIImage *image,它保存您想要着色或着色的图像。

Draw a color onto the object like this:

    // first draw image
    [self.image drawInRect:rect];

    // prepare the context to draw into
    CGContextRef context = UIGraphicsGetCurrentContext();

    // set the blend mode and draw rectangle on top of image
    CGContextSetBlendMode(context, kCGBlendModeColor);
    CGContextClipToMask(context, self.bounds, image.CGImage); // this restricts drawing to within alpha channel
    CGContextSetRGBFillColor(context, 0.75, 0.0, 0.0, 1.0); // this is your color,  a light reddish tint
    CGContextFillRect(context, rect);       

I put this into the drawRect: method of a custom UIView. That UIView has an ivar, UIImage *image that holds the image you want to tint or color.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文