Hiren
Hiren

Reputation: 12780

how to give UIImage negative color effect

This is my first question.
I have an app in which i want to change the image effect look like negative color effect

Upvotes: 1

Views: 1791

Answers (2)

MCKapur
MCKapur

Reputation: 9157

Make a method:

 - (UIImage *)makeImageNegative:(UIImage *)image{
 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();    
 return returnImage;
 }

Upvotes: 4

Hiren
Hiren

Reputation: 12780

it's so simple you have to choose image and set the image in the below code

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

Upvotes: 6

Related Questions