Question

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

Was it helpful?

Solution

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

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

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.

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.

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

OTHER TIPS

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;
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top