1
votes

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);    
2
i hope nothing wrong with the code can u explain little bit more.... - Arun
I think the pictures are quite clear ! the transparent area fills with white color that's the problem , @Spynet - Mc.Lover

2 Answers

0
votes

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