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

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

问题描述



(PNG格式,所以有alpha分量)

我有一个灰度gem顶视图。



我想从这个图像创建12个小尺寸的按钮,每个按钮都有不同的颜色。



为了整洁,我想在代码中做这个,而不是在外部在一些艺术包。

任何人都可以提供一个方法(甚至一些代码)做到这一点?



PS我知道如何在GL中使用一个荒谬的数量的代码,我希望有一个更简单的方式使用核心图形/核心动画编辑:b
$ b

编辑:工作解决方案,感谢从下面的精彩回答

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

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

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

//绘制宝石
[uiGem drawInRect:rect];

//覆盖一个红色的矩形
CGContextSetBlendMode(X,kCGBlendModeColor);
CGContextSetRGBFillColor(X,0.9,0,0,1);
CGContextFillRect(X,rect);

//重画宝石
[uiGem drawInRect:rect
blendMode:kCGBlendModeDestinationIn
alpha:1.];

image = UIGraphicsGetImageFromCurrentImageContext();
}
UIGraphicsEndImageContext();


解决方案

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


I have a greyscale gem top view.

(PNG format, so has alpha component)

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 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

EDIT: 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();
    }

解决方案

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天全站免登陆