IPhone SDK Invert UIImage Colors

I have an iPhone app that allows a person to draw something on the screen with their finger. They draw a white line on a black background. However, when I upload this image to the web server, the line itself is white. I am trying to work through the main graphics libraries in search of a way to invert the color of the drawn image. There is no background, so it should be transparent. Performing an inversion of the image should change white to black.

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

Greetings

+4
source share
2 answers

I ended up finding a good way to do this. Instead of just making the background of the original image transparent, I make it black. Now on a black background white lines. Then it was just a matter:

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 a white line on a black background. Thanks for the help David Sousie

+14
source

Yes. You should be able to do this in CALayer by calling CGContext or directly with a buffer on CGImage. There are other examples of how to go back and forth while saving an image from a UIView also in Stackoverflow.

-1
source

Source: https://habr.com/ru/post/1301293/


All Articles