简体   繁体   English

如何在UIImage中使一种颜色透明

[英]How to make one colour transparent in UIImage

I want to change a colour in UIimage to transparent I am using below code to change black colour to transparent 我想将UIimage中的颜色更改为透明我正在使用以下代码将黑色更改为透明

-(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 谁能帮我如何使颜色变透明

From the docs : 文档

components 组件

An array of color components that specify a color or range of colors to mask the image with. 颜色组件的数组,这些组件指定用于掩盖图像的颜色或颜色范围。 The array must contain 2N values { min 1 , max 1 , ... min[N], max[N] } where N is the number of components in color space of image. 数组必须包含2N个值{min 1 ,max 1 ,... min [N],max [N]},其中N是图像颜色空间中的分量数。 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). 如果图像具有整数像素分量,则每个值必须在[0 .. 2 ** bitsPerComponent-1]范围内(其中bitsPerComponent是图像的位数/分量)。 If image has floating-point pixel components, then each value may be any floating-point number which is a valid color component. 如果图像具有浮点像素分量,则每个值可以是任何有效的颜色分量的浮点数。

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). 用简单的英语来说,如果您有一个典型的RGB图像(RGB是色彩空间的名称),则您有3个分量:R(红色),G(绿色)和B(蓝色),每个分量的范围从0到255(2 ** 8-1,假设每个组件8位)。

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. 因此, colorMasking定义要透明的每个组件的值的范围,即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值在您在colorMasking设置的范围之间的值。

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)). 在您的示例中,该数组全为零,因为您想使黑色透明(请记住,RGB中的黑色为(0,0,0))。

Try this- 尝试这个-

-(UIImage *)changeWhiteColorTransparent: (UIImage *)image
{
   CGImageRef rawImageRef=image.CGImage;    
   const float colorMasking[6] = {222, 255, 222, 255, 222, 255};    
   UIGraphicsBeginImageContext(image.size);
   CGImageRef maskedImageRef=CGImageCreateWithMaskingColors(rawImageRef, colorMasking);
    {
        //if in iPhone            
   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();    
    return result;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM