1

I am trying to invert colors on UIImageView, but the problem is when I try to invert colors , the transparent area fills with white color :

enter image description here

only white areas should be inverted to black color , but the result is :

enter image description here

here is my codes :

- (void)invertColorWithImage:(UIImageView*)image {


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

}

I chante this line to clear color but nothing changed :

CGContextSetFillColorWithColor(UIGraphicsGetCurrentContext(),[UIColor whiteColor].CGColor);    
iOS.Lover
  • 5,923
  • 21
  • 90
  • 162

2 Answers2

0

From reading the code it looks as though this could be the culprit

CGContextSetFillColorWithColor(UIGraphicsGetCurrentContext(),[UIColor whiteColor].CGColor);    
CGContextFillRect(UIGraphicsGetCurrentContext(), CGRectMake(0, 0, image.image.size.width, image.image.size.height));

You're setting the fill colour to white, then on the next line saying to fill in the whole image.

I've not had a chance to run the code but that looks likely

JConway
  • 569
  • 5
  • 13
0

I found this method and works good : http://coffeeshopped.com/2010/09/iphone-how-to-dynamically-color-a-uiimage

iOS.Lover
  • 5,923
  • 21
  • 90
  • 162