10

How can I clear out the magenta part of an UIImage and make it transparent?

I've looked through numerous answers and links on SO and nothing works (e.g. How to make one color transparent on a UIImage? answer 1 removes everything but red, answer 2 apparently doesn't work because of Why is CGImageCreateWithMaskingColors() returning nil in this case?).

Update:

If I use CGImageCreateWithMaskingColors with the UIImage I get a nil value. If I remove the alpha channel (I represent the image as JPEG and read it back) CGImageCreateWithMaskingColors returns an image painted with a black background.

Update2, the code:

Returning nil:

const float colorMasking[6] = {222, 255, 222, 255, 222, 255};
CGImageRef imageRef = CGImageCreateWithMaskingColors(anchorWithMask.CGImage, colorMasking);

NSLog(@"image ref %@", imageRef);
// this is going to return a nil imgref.

UIImage *image = [UIImage imageWithCGImage:imageRef];

Returning an image with black background (which is normal since there is not alpha channel):

UIImage *inputImage = [UIImage imageWithData:UIImageJPEGRepresentation(anchorWithMask, 1.0)];

const float colorMasking[6] = {222, 255, 222, 255, 222, 255};
CGImageRef imageRef = CGImageCreateWithMaskingColors(inputImage.CGImage, colorMasking);

NSLog(@"image ref %@", imageRef);
// imgref is NOT nil.

UIImage *image = [UIImage imageWithCGImage:imageRef];

Update3:

I got it working by adding the alpha channel after the masking process.

Community
  • 1
  • 1
MB.
  • 4,167
  • 8
  • 52
  • 79

3 Answers3

4

I made this static function that removes the white background you can use it replacing the mask with the color range you want to remove:

+ (UIImage*) processImage :(UIImage*) image
{
    const float colorMasking[6]={222,255,222,255,222,255};
    CGImageRef imageRef = CGImageCreateWithMaskingColors(image.CGImage, colorMasking);
    UIImage* imageB = [UIImage imageWithCGImage:imageRef];
    CGImageRelease(imageRef);
    return imageB;
}
Khaled Annajar
  • 15,542
  • 5
  • 34
  • 45
  • It is working on my project and I am removing the white color range. I think you specify colors range like in this one I use from 222 -> 255 for Red to be removed. I may not be understanding it 100% but you could try it again with the range you need. – Khaled Annajar Feb 03 '13 at 08:32
  • @khaledannajar I have used this code it's work perfect but still some white pixcel can't remove from border of blank line which I have drawn programatically in my app – kb920 May 14 '14 at 06:38
  • @keyur bhalodiya You can try changing the ranges of the white color (making it wider) other than that I don't know how to help you. :( – Khaled Annajar May 14 '14 at 16:28
4
UIImage *image = [UIImage imageNamed:@"image.png"];

const float colorMasking[6] = {1.0, 1.0, 0.0, 0.0, 1.0, 1.0};
image = [UIImage imageWithCGImage: CGImageCreateWithMaskingColors(image.CGImage, colorMasking)];

You receive nil, because parameters, that you send is invalid. If you open Apple documentation, you will see this:

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

You can quickly open documentation by holding Option + Mouse Click on some function or class, like CGImageCreateWithMaskingColors.

ArtFeel
  • 11,701
  • 4
  • 29
  • 41
  • I just tried this and it's returning a completely transparent uiimage. – MB. Nov 29 '11 at 19:01
  • This is how the input image looks like: http://i.imgur.com/lxJdm.png (this is the png version, internally it's an UIImage from UIImagePicker) – MB. Nov 29 '11 at 19:08
0

I did this in CIImage with the post-processing from Vision being a pixelBuffer:

    let ciImage = CIImage(cvPixelBuffer: pixelBuffer)
    let filteredImage = ciImage.applyingFilter("CIMaskToAlpha")
    self.picture.image = UIImage(ciImage: filteredImage)
Xavier Chia
  • 203
  • 3
  • 5