1

我试图在 UIImageView 上反转颜色,但问题是当我尝试反转颜色时,透明区域填充为白色:

在此处输入图像描述

只有白色区域应反转为黑色,但结果是:

在此处输入图像描述

这是我的代码:

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

}

我高呼这条线以清除颜色,但没有任何改变:

CGContextSetFillColorWithColor(UIGraphicsGetCurrentContext(),[UIColor whiteColor].CGColor);    
4

2 回答 2

0

从阅读代码看来,这可能是罪魁祸首

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

您将填充颜色设置为白色,然后在下一行表示填充整个图像。

我没有机会运行代码,但看起来很可能

于 2013-06-12T10:16:38.573 回答
0

我发现这种方法效果很好: http ://coffeeshopped.com/2010/09/iphone-how-to-dynamically-color-a-uiimage

于 2013-06-12T13:03:00.060 回答