iPhone SDK反转UIImage的颜色 [英] iPhone SDK Invert the Colors of a UIImage

查看:155
本文介绍了iPhone SDK反转UIImage的颜色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个iPhone应用程序,允许一个人用手指在屏幕上绘制东西。他们画与在黑背景的一条白线。但是,当我将此图像上传到Web服务器时,该行本身为白色。我一直在努力通过核心图形库,寻找一种方式来反转绘制图像的颜色。没有背景设置,所以它应该是透明的。

I've got an iPhone app that allows a person to draw something on the screen with their finger. They draw with a white line on a black background. However, when I upload this image to the web server, the line itself is white. I have been trying to work through the core graphics libraries in search of a way to invert the colour of the drawn image. There is no background set so it should be transparent. Doing an image invert should swap the white to black.

有没有人知道是否可以通过核心图形库实现?

Does anyone know if this is possible through the core graphics library?

干杯

推荐答案

我最终找到了一个好办法。而不是简单地使原始图像的背景透明,我使它黑色。现在在黑色背景上有白线。那么它只是一个问题:

I ended up finding a good way of doing it. Instead of simply making the background of the original image transparent, I make it black. Now there are white lines on a black background. Then it was simply a matter of doing:

UIGraphicsBeginImageContext(image.size);
CGContextSetBlendMode(UIGraphicsGetCurrentContext(), kCGBlendModeCopy);
[image drawInRect:CGRectMake(0, 0, image.size.width, image.size.height)];
CGContextSetBlendMode(UIGraphicsGetCurrentContext(), kCGBlendModeDifference);
CGContextSetFillColorWithColor(UIGraphicsGetCurrentContext(),[UIColor whiteColor].CGColor);
CGContextFillRect(UIGraphicsGetCurrentContext(), CGRectMake(0, 0, image.size.width, image.size.height));
UIImage *returnImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();

变量image是黑色背景上带有白线的原始图像。感谢您的帮助David Sowsy

The variable 'image' is the original image with white line on black background. Thanks for your help David Sowsy

这篇关于iPhone SDK反转UIImage的颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆