【问题标题】:Inverting colors on UIImageView在 UIImageView 上反转颜色
【发布时间】:2013-06-11 19:05:33
【问题描述】:

我试图在 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);    

【问题讨论】:

  • 我希望代码没有问题,你能多解释一下......
  • 我觉得图片很清晰!透明区域用白色填充,这就是问题所在,@Spynet

标签: iphone ios objective-c ipad


【解决方案1】:

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

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

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

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

【讨论】:

    【解决方案2】:

    【讨论】:

      猜你喜欢
      • 2012-02-16
      • 2014-04-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-06-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多