【发布时间】:2009-08-27 15:09:53
【问题描述】:
我有一张来自数据库的小图片,需要稍微改变图片的平均颜色。
这是一个 CGImageRef,我想创建一个 CGContext,将图像绘制到这个上下文,然后以某种方式更改位图数据并最终渲染它。 但是如何更改颜色信息?
感谢您的帮助!
【问题讨论】:
我有一张来自数据库的小图片,需要稍微改变图片的平均颜色。
这是一个 CGImageRef,我想创建一个 CGContext,将图像绘制到这个上下文,然后以某种方式更改位图数据并最终渲染它。 但是如何更改颜色信息?
感谢您的帮助!
【问题讨论】:
请查看this Apple Q&A on pixel data manipulation,了解有关如何处理此问题的详细信息。
【讨论】:
像这样在对象上绘制颜色:
// 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 包含您要着色或着色的图像。
【讨论】: