2012-10-29 141 views
2

我有一个图像,我用我的iOS设备,包含几行字和一点背景色(红色)。我希望图像只显示给用户(他们会做类似的事情)。我无法以任何方式使用UIImageView,因此仅仅将图像放在背景上并不适用。透明度iOS

所以我想知道如果有人能告诉我哪种下面的方法会给我最好的结果,如果可能的话,例子会很好。我已经在下面的代码中发布了我曾尝试过的小运气(选项2)。

  1. 使背景白色所以只有词出现
  2. 从图像预先取出红色

由于

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)]; 

回答

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]; 

我已删除的浅灰色颜色来自我的图像背景。对于RGB,颜色范围在225和255之间。现在看起来很透明。

+0

非常感谢!但是,你能告诉我如何去除除黑色或灰色之外的所有颜色(这些是图像中的文本)的值吗?现在,当我运行你的代码时,它似乎没有太大的影响。再次感谢 –

+0

您可以将RGB的最小范围更改为1,以便只保留黑色并将所有其他颜色从图像中移除。 const float colorMasking [6] = {1.0,255.0,1.0,255.0,1.0,255.0}; – AJS

+0

感谢您的快速回复。当我这样做时,它会让我的整个屏幕(图像)变成白色。你知道为什么吗?无论如何,我会接受的,但如果你能回答,这将意味着很多。谢谢 –