iPhone: How to change the color of an image

谁都会走 提交于 2019-12-12 10:20:08

问题


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. But how can I alter the color information?

Thanks for your help!


回答1:


Check out this Apple Q&A on pixel data manipulation for details on how you might go about that.




回答2:


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. That UIView has an ivar, UIImage *image that holds the image you want to tint or color.



来源:https://stackoverflow.com/questions/1341716/iphone-how-to-change-the-color-of-an-image

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!