简体   繁体   English

iPhone:如何更改图像的颜色

[英]iPhone: How to change the color of an image

I have a small image from a database and the image's average color need to be altered slightly. 我有一个来自数据库的小图像,该图像的平均颜色需要稍作更改。

It's a CGImageRef and I thought of creating a CGContext , drawing the image to this context, then subsequently changing the bitmap data somehow and finally rendering it. 这是一个CGImageRef ,我想创建一个CGContext ,将图像绘制到此上下文,然后以某种方式更改位图数据并最终渲染它。 But how can I alter the color information? 但是,如何更改颜色信息?

Thanks for your help! 谢谢你的帮助!

查看此Apple关于像素数据操作的问答,以了解有关如何进行处理的详细信息。

Draw a color onto the object like this: 像这样在对象上绘制颜色:

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

I put this into the drawRect: method of a custom UIView. 我将其放入自定义UIView的drawRect:方法中。 That UIView has an ivar, UIImage *image that holds the image you want to tint or color. 该UIView具有一个ivar,即UIImage * image,该图像包含要着色或着色的图像。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM