如何更改UIImage的颜色 [英] How to change the color of an UIImage

查看:276
本文介绍了如何更改UIImage的颜色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不想改变UIImage的backgroundColor,而是改变整个图像的颜色。

I want not to change the backgroundColor of an UIImage,
but rather to change the color of the whole image.

因为我有标准表格,我可以从popover移动到视图。

在我的应用程序中,可以将图像修改为一个大图像,即一张桌子。

此外我希望将颜色改为白褐色或其他什么。

Because I have got standard forms which I can move from popover to the view.
In my App it is possible to tinker the images to one big image, i.e a table.
Moreover I want change the color to white brown or what else.

但问题是:我只能更改backgroundColor

有没有办法改变UIImage的颜色?

But the problem is: I can only change the backgroundColor
Is there any way to change the color of the UIImage?

提前谢谢

推荐答案

UIImage *image = [UIImage imageNamed:@"triangle.png"];

CGRect rect = CGRectMake(0, 0, image.size.width, image.size.height);
UIGraphicsBeginImageContext(rect.size);
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextClipToMask(context, rect, image.CGImage);
CGContextSetFillColorWithColor(context, [[UIColor redColor] CGColor]);
CGContextFillRect(context, rect);
UIImage *img = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();

UIImage *flippedImage = [UIImage imageWithCGImage:img.CGImage 
                                            scale:1.0 orientation: UIImageOrientationDownMirrored];

yourUIImageView.image = flippedImage;

这篇关于如何更改UIImage的颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆