iPhone SDK Invert the Colors of a UIImage

喜欢而已 提交于 2019-12-10 16:45:11

问题


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?

Cheers


回答1:


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

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




回答2:


Yes. You should be able to do this in a CALayer by the calls to CGContext, or directly with a buffer on CGImage. There are other examples on how to go back and forth with save image from a UIView also on Stackoverflow.



来源:https://stackoverflow.com/questions/2263467/iphone-sdk-invert-the-colors-of-a-uiimage

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!