【问题标题】:iPhone: How to change the color of an imageiPhone:如何更改图像的颜色
【发布时间】:2009-08-27 15:09:53
【问题描述】:

我有一张来自数据库的小图片,需要稍微改变图片的平均颜色。

这是一个 CGImageRef,我想创建一个 CGContext,将图像绘制到这个上下文,然后以某种方式更改位图数据并最终渲染它。 但是如何更改颜色信息?

感谢您的帮助!

【问题讨论】:

    标签: iphone colors cgimage


    【解决方案1】:

    请查看this Apple Q&A on pixel data manipulation,了解有关如何处理此问题的详细信息。

    【讨论】:

      【解决方案2】:

      像这样在对象上绘制颜色:

          // first draw image
          [self.image drawInRect:rect];
      
          // prepare the context to draw into
          CGContextRef context = UIGraphicsGetCurrentContext();
      
          // set the blend mode and draw rectangle on top of image
          CGContextSetBlendMode(context, kCGBlendModeColor);
          CGContextClipToMask(context, self.bounds, image.CGImage); // this restricts drawing to within alpha channel
          CGContextSetRGBFillColor(context, 0.75, 0.0, 0.0, 1.0); // this is your color,  a light reddish tint
          CGContextFillRect(context, rect);       
      

      我把它放到了一个自定义 UIView 的 drawRect: 方法中。该 UIView 有一个 ivar,UIImage *image 包含您要着色或着色的图像。

      【讨论】:

      • 谢谢,但我需要自己更改每个像素。
      猜你喜欢
      • 1970-01-01
      • 2020-10-10
      • 1970-01-01
      • 2020-09-20
      • 2013-03-29
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多