如何在UIImage中使一种颜色透明 [英] How to make one colour transparent in UIImage

查看:104
本文介绍了如何在UIImage中使一种颜色透明的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想将UIimage中的颜色更改为透明
我使用下面的代码将黑色颜色更改为透明

I want to change a colour in UIimage to transparent I am using below code to change black colour to transparent

-(void)changeColorToTransparent: (UIImage *)image{
    CGImageRef rawImageRef = image.CGImage;
    const float colorMasking[6] = { 0, 0, 0, 0, 0, 0 };
    UIGraphicsBeginImageContext(image.size);
    CGImageRef maskedImageRef =  CGImageCreateWithMaskingColors(rawImageRef, colorMasking);
   {
       CGContextTranslateCTM(UIGraphicsGetCurrentContext(), 0.0, image.size.height);
       CGContextScaleCTM(UIGraphicsGetCurrentContext(), 1.0, -1.0);
   }

   CGContextDrawImage(UIGraphicsGetCurrentContext(), CGRectMake(0, 0, image.size.width, image.size.height), maskedImageRef);
   UIImage *result = UIGraphicsGetImageFromCurrentImageContext();
   CGImageRelease(maskedImageRef);
   UIGraphicsEndImageContext();
 }

工作正常..
但是我想画一点在图像上通过选择颜色形式颜色选择器然后想要使该点透明..
我不知道如何在下面的行中给出颜色掩盖的值

Its working fine.. But i want to draw a point on image by picking the colour form colour picker and then wants to make that point transparent.. I dont know how to give values in colour masking in below line

const float colorMasking[6] = { 0, 0, 0, 0, 0, 0 };

任何人都可以帮助我如何使颜色变得透明

Can any one please help me how we can make a colour to transparent

推荐答案

来自 docs


components

components

一组颜色组件,用于指定掩盖图像的颜色或颜色范围
。该数组必须包含2N个值{min 1
max 1 ,... min [N],max [N]}其中N是
颜色空间中的组件数图像组件中的每个值必须是有效的图像
样本值。如果图像具有整数像素分量,则每个值
必须在[0 .. 2 ** bitsPerComponent - 1]范围内(其中
bitsPerComponent是图像的位数/分量)。如果图像
具有浮点像素组件,则每个值可以是任何
浮点数,它是有效的颜色组件。

An array of color components that specify a color or range of colors to mask the image with. The array must contain 2N values { min1, max1, ... min[N], max[N] } where N is the number of components in color space of image. Each value in components must be a valid image sample value. If image has integer pixel components, then each value must be in the range [0 .. 2**bitsPerComponent - 1] (where bitsPerComponent is the number of bits/component of image). If image has floating-point pixel components, then each value may be any floating-point number which is a valid color component.

简单来说,如果你有一个典型的RGB图像(RGB是颜色空间的名称),那么你有3个组件:R(红色),G(绿色)和B(蓝色) ,每个范围从0到255(2 ** 8 - 1,假设每个组件8位)。

In plain English, if you have a typical RGB image (RGB is the name of the color space), then you have 3 components: R (red), G (green), and B (blue), each one ranging from 0 to 255 (2**8 - 1, assuming 8 bits per component).

所以, colorMasking 定义要使其透明的每个组件的值范围,即, colorMasking 中的第一个元素是最小红色组件,第二个元素是最大红色分量,第三个是最小绿色分量,依此类推。

So, colorMasking defines the ranges of values for each component that you want to make transparent, ie., the first element in colorMasking is the minimum red component, the second one is the maximum red component, the third one is the minimum green component, and so forth.

结果图像将是输入图像,其中一些像素是透明的。哪个像素?那些RGB值介于 colorMasking 中设置的范围之间。

The result image will be the input image with some pixels transparent. Which pixels? Those whose RGB values lie between the ranges you set in colorMasking.

在您的示例中,数组全部为零因为你想让黑色透明(记住,RGB中的黑色是(0,0,0))。

In your example, the array is all zeros because you want to make black transparent (and remember, black color in RGB is (0,0,0)).

这篇关于如何在UIImage中使一种颜色透明的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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