如何赋予UIImage负面色彩效果

时间:2012-03-30 09:57:55

标签: iphone objective-c ios ipad ios5

这是我的第一个问题 我有一个应用程序,我想在其中更改图像效果看起来像负色效果

2 个答案:

答案 0 :(得分:6)

这很简单,您必须选择图像并在下面的代码中设置图像

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

答案 1 :(得分:4)

制作方法:

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