简体   繁体   English

iPhone SDK:更改图片颜色

[英]Iphone SDK: change image color

In cocos2d, there's an option to "change" a sprite's color. 在cocos2d中,有一个选项可以“改变”精灵的颜色。 For example: I have a white image, then I execute the code and the image turns to any color that I specify. 例如:我有一幅白色的图像,然后执行代码,该图像变为我指定的任何颜色。 How can I do the same thing without using the cocos2d libraries? 不使用cocos2d库,如何做同样的事情?

I know it's possible and I did such a thing myself about 5 months ago, but in typical fashion I somehow managed to lose the code. 我知道这是有可能的,大约5个月前我自己做了这样的事情,但是我以某种典型的方式设法丢失了代码。

try this 尝试这个

UIImage *newImage = [_imgView.image imageWithRenderingMode:UIImageRenderingModeAlwaysTemplate];
UIGraphicsBeginImageContextWithOptions(image.size, NO, newImage.scale);
[yourTintColor set];
[newImage drawInRect:CGRectMake(0, 0, image.size.width, newImage.size.height)];
newImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();

_imgView.image = newImage;

First set the image in UIImageView then use below code snippet. 首先在UIImageView中设置图像,然后使用以下代码段。

imgView.image = [imgView.image imageWithRenderingMode:UIImageRenderingModeAlwaysTemplate];
[imgView setTintColor:[UIColor redColor]];

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

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