Alex G
Alex G

Reputation: 2309

Transparency iOS

I have an image that I took with my iOS device that contains a few lines of words and little bit of background colour (red). I want the image to only display the words to the user (they will be doing something similar). I cannot use an UIImageView in any way so simply taking the image and putting it on the background will not work.

So I was wondering if someone could tell me which of the following methods would give me the best results, if they are possible and example would be nice. I have posted code below of what I have tried to do with little luck (option 2).

  1. Make the background white so only the words appear
  2. Remove the color red from the image

Thanks in advance

 UIImage *image = [UIImage imageNamed:@"pic1.jpg"];

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

Upvotes: 2

Views: 1088

Answers (1)

AJS
AJS

Reputation: 1403

UIImage *image = [UIImage imageNamed:@"image1.jpg"];
UIImage *inputImage = [UIImage imageWithData:UIImageJPEGRepresentation(image, 1.0)];

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

UIImage *img2 = [UIImage imageWithCGImage:imageRef];

I have removed the light gray color from the background of my image.The range of color is given between 225 and 255 for RGB. Now it appears transparent.

Upvotes: 2

Related Questions