2

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)];
Alex G
  • 2,299
  • 5
  • 37
  • 54

1 Answers1

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

AJS
  • 1,403
  • 12
  • 17
  • Thanks so much! But can you tell me the values for how to remove all the colours except black or grey (those being the text in the image)? Right now it does not seem to have much of an effect when I run your code. Thanks again – Alex G Oct 29 '12 at 07:47
  • You can change the minimum range to 1 for RGB so that only black color will remain and all other colors will be removed from the image. const float colorMasking[6] = {1.0, 255.0, 1.0, 255.0, 1.0, 255.0}; – AJS Oct 29 '12 at 08:02
  • Thanks for the quick reply. When I do that, it makes my entire screen (image) white. Do you know why? I will accept anyway but it would be mean a lot if you could answer. Thanks – Alex G Oct 29 '12 at 08:07
  • Or sorry to bother but could you provide the colorMasking numbers for everything but RED? I am not quite sure how it works. Thanks – Alex G Oct 29 '12 at 08:41
  • it's R, G, B right? So everything but red should be {0.0, 0.0, 0.0, 255.0, 0.0, 255.0} ? – powerj1984 Feb 11 '14 at 15:04
  • Thanks dude... Above Code is working fine for me to create a transparent image with white background. – Himanshu Mahajan Jun 12 '14 at 06:51