iOS:将 RGB 滤镜应用于灰度 PNG [英] iOS: Applying a RGB filter to a greyscale PNG

查看:24
本文介绍了iOS:将 RGB 滤镜应用于灰度 PNG的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个灰度宝石顶视图.

I have a greyscale gem top view.

(PNG 格式,所以有 alpha 组件)

(PNG format, so has alpha component)

我想根据这张图片创建 12 个小尺寸按钮,每个按钮的颜色都不同.

I would like to create 12 small size buttons, each in a different colour, from this image.

为了整洁起见,我想在代码中而不是在某些艺术包中进行此操作.

For the sake of tidiness, I would like to do this within the code rather than externally in some art package.

谁能提供一个方法(甚至一些代码)来做到这一点?

Can anyone provide a method (or even some code) for doing this?

PS 我知道如何在 GL 中使用大量代码来完成它,我希望有一种使用核心图形/核心动画的更简单的方法

PS I am aware of how to do it in GL using a ridiculous amount of code, I'm hoping there is a simpler way using core graphics / core animation

工作解决方案,感谢以下答案的精彩

Working solution, thanks to awesomeness from below answer

    CGSize targetSize = (CGSize){100,100};
    UIImage* image;
    {
        CGRect rect = (CGRect){ .size = targetSize };

        UIGraphicsBeginImageContext( targetSize );
        {
            CGContextRef X = UIGraphicsGetCurrentContext();

            UIImage* uiGem = [UIImage imageNamed: @"GemTop_Dull.png"];

            // draw gem
            [uiGem drawInRect: rect];

            // overlay a red rectangle
            CGContextSetBlendMode( X, kCGBlendModeColor ) ;
            CGContextSetRGBFillColor ( X,  0.9, 0, 0,  1 );
            CGContextFillRect ( X, rect );

            // redraw gem 
            [uiGem drawInRect: rect
                    blendMode: kCGBlendModeDestinationIn
                        alpha: 1. ];

            image = UIGraphicsGetImageFromCurrentImageContext();
        }
        UIGraphicsEndImageContext();
    }

推荐答案

最简单的方法是将图像绘制成 RGB 颜色空间 CGBitmapContext,使用CGContextSetBlendMode设置kCGBlendModeColor,以及然后用纯色绘制它(例如用 CGContextFillRect).

The easiest way to do it is to draw the image into an RGB-colorspaced CGBitmapContext, use CGContextSetBlendMode to set kCGBlendModeColor, and then draw over it with a solid color (e.g. with CGContextFillRect).

这篇关于iOS:将 RGB 滤镜应用于灰度 PNG的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆