我怎样才能改变图像tintColor

我从服务器接收图像,然后基于用户select的颜色,图像颜色将会改变。

我尝试了以下内容:

_sketchImageView.image = [_sketchImageView.image imageWithRenderingMode:UIImageRenderingModeAlwaysTemplate]; [_sketchImageView setTintColor:color]; 

我得到了与我目标相反的目标(UIImage外部的白色用所选颜色着色)。

什么问题?

我需要在这个问题上做同样的事情,所提供的解决scheme并不能解决我的问题。 如何更改iOS和WatchKit中的图像tintColor

尝试为自己生成新的图像

 UIImage *newImage = [_sketchImageView.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(); _sketchImageView.image = newImage; 

并使用它。

祝你好运

=======更新=======

这个解决scheme只会改变所有像素的图像的颜色。

例如:我们有一本书图片: http : //pngimg.com/upload/book_PNG2113.png

在这里输入图像说明

在运行上面的代码之后(exp: TintColor是RED)。 我们有:

在这里输入图像说明

所以: 你的形象是如何取决于你如何devise的

在Swift中你可以使用这个扩展:[基于@ VietHung的objective-c解决scheme]

 extension UIImage { func imageWithColor(color: UIColor) -> UIImage? { var image = imageWithRenderingMode(.AlwaysTemplate) UIGraphicsBeginImageContextWithOptions(size, false, scale) color.set() image.drawInRect(CGRect(x: 0, y: 0, width: size.width, height: size.height)) image = UIGraphicsGetImageFromCurrentImageContext() UIGraphicsEndImageContext() return image } } 

在swift 2.0中,你可以使用这个

 let image = UIImage(named:"your image name")?.imageWithRenderingMode(.AlwaysTemplate) let yourimageView.tintColor = UIColor.redColor() yourimageView.image = image 

在swift 3.0中,你可以使用这个

 let image = UIImage(named:"your image name")?.withRenderingMode(.alwaysTemplate) let yourimageView.tintColor = UIColor.red yourimageView.image = image 

尝试这样的事情

 UIImage *originalImage = _sketchImageView.image UIImage *newImage = [originalImage imageWithRenderingMode:UIImageRenderingModeAlwaysTemplate]; UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0,0,50,50)]; // your image size imageView.tintColor = [UIColor redColor]; // or whatever color that has been selected imageView.image = newImage; _sketchImageView.image = imageView.image; 

希望这可以帮助。

你可以试试:

  _sketchImageView.image = [self imageNamed:@"imageName" withColor:[UIColor blackColor]]; - (UIImage *)imageNamed:(NSString *)name withColor:(UIColor *)color { // load the image //NSString *name = @"badge.png"; UIImage *img = [UIImage imageNamed:name]; // begin a new image context, to draw our colored image onto UIGraphicsBeginImageContext(img.size); // get a reference to that context we created CGContextRef context = UIGraphicsGetCurrentContext(); // set the fill color [color setFill]; // translate/flip the graphics context (for transforming from CG* coords to UI* coords CGContextTranslateCTM(context, 0, img.size.height); CGContextScaleCTM(context, 1.0, -1.0); // set the blend mode to color burn, and the original image CGContextSetBlendMode(context, kCGBlendModeColorBurn); CGRect rect = CGRectMake(0, 0, img.size.width, img.size.height); CGContextDrawImage(context, rect, img.CGImage); // set a mask that matches the shape of the image, then draw (color burn) a colored rectangle CGContextClipToMask(context, rect, img.CGImage); CGContextAddRect(context, rect); CGContextDrawPath(context,kCGPathFill); // generate a new UIImage from the graphics context we drew onto UIImage *coloredImg = UIGraphicsGetImageFromCurrentImageContext(); UIGraphicsEndImageContext(); //return the color-burned image return coloredImg; } 

在Swift 3.0中,你可以使用这个扩展:[基于@ VietHung的objective-c解决scheme]

 extension UIImage { func imageWithColor(_ color: UIColor) -> UIImage? { var image = imageWithRenderingMode(.alwaysTemplate) UIGraphicsBeginImageContextWithOptions(size, false, scale) color.set() image.draw(in: CGRect(x: 0, y: 0, width: size.width, height: size.height)) image = UIGraphicsGetImageFromCurrentImageContext() UIGraphicsEndImageContext() return image } } 

尝试在图像视图的超视图上设置色调颜色。 例如[self.view setTintColor:color];

对于Swift 3.0,我制作了一个名为TintedUIImageView的UIImageView的自定义子类。 现在,图像使用界面生成器或代码中设置的任何色调

 class TintedUIImageView: UIImageView { override func awakeFromNib() { if let image = self.image { self.image = image.withRenderingMode(.alwaysTemplate) } } } 

以下是我如何在Swift中使用IOS 9中的色彩。

 //apply a color to an image //ref - http://stackoverflow.com/questions/28427935/how-can-i-change-image-tintcolor //ref - https://www.captechconsulting.com/blogs/ios-7-tutorial-series-tint-color-and-easy-app-theming func getTintedImage() -> UIImageView { var image : UIImage; var imageView : UIImageView; image = UIImage(named: "someAsset")!; let size : CGSize = image.size; let frame : CGRect = CGRectMake((UIScreen.mainScreen().bounds.width-86)/2, 600, size.width, size.height); let redCover : UIView = UIView(frame: frame); redCover.backgroundColor = UIColor.redColor(); redCover.layer.opacity = 0.75; imageView = UIImageView(); imageView.image = image.imageWithRenderingMode(UIImageRenderingMode.Automatic); imageView.addSubview(redCover); return imageView; } 

你可以做的一件事就是将你的图像添加到XCode中的Assets文件夹中,然后将渲染模式改为Template Image,所以无论何时改变UIImageView的色调,它都会自动改变图像。

检查此链接 – > https://www.google.co.in/url?sa=i&rct=j&q=&esrc=s&source=images&cd=&cad=rja&uact=8&ved=0ahUKEwiM0YXO0ejTAhUIQ48KHfGpBpgQjRwIBw&url=https%3A%2F%2Fkrakendev.io% 2Fblog%2F4-的Xcode-资产类别-秘密-你-需要对知识和磅= AFQjCNGnAzVn92pCqM8612o1R0J9q1y7cw与UST = 1494619445516498