2015-02-10 63 views
24

我从服务器接收图像,然后基于用户选择的颜色,图像颜色将发生变化。如何更改图像tintColor

我试过如下:

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

我得到了我的目标相反的(白色的UIImage外面是彩色的与所选择的颜色)。

出了什么问题?

我需要在这个问题上做同样的事情,所提供的解决方案并不能解决我的问题。 How can I change image tintColor in iOS and WatchKit

+0

如果你想填补图像与一些颜色,你需要更先进的解决方案。像这样:https://github.com/mxcl/UIImageWithColor – orkenstein 2015-02-10 09:19:56

+0

我认为你需要背景颜色的效果,试试'[_sketchImageView setBackgroundColor:color]' – Saif 2015-02-10 09:22:20

+0

我以前试过这个解决方案,它给出了相同的结果。 // UIImage * img = [UIImage resizableImageWithColor:color cornerRadius:10]; // UIImage * img = [UIImage imageWithColor:color size:CGSizeMake(20,20)]; UIImage * img = [UIImage imageWithColor:color]; _sketchImageView.image = img; – user2168496 2015-02-10 09:36:34

回答

45

尝试生成新的形象为自己

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; 

并使用它。

好运

======= ======= UPDATE

该解决方案将只有所有像素的图像的变色。

实施例:我们有一个书图像:http://pngimg.com/upload/book_PNG2113.png

enter image description here

并运行上面的代码后(EXP:TintColor是红色)。我们有:

enter image description here

SO:你的形象是如何取决于你如何把它设计

+0

thanx很多,可以说我有一本书的图像,这个解决方案给图书的边界以外的图像着色..我想的是相反的,即书本身是彩色的。 任何想法为什么会发生这种情况? – user2168496 2015-02-11 08:08:07

+0

我认为你需要重新设计你的书籍图像。我刚刚编辑了我的帖子。看到它 – VietHung 2015-02-11 08:34:46

+0

Yeah.yessss ..我试图你的形象,它工作正常:)我真的很感谢 – user2168496 2015-02-11 08:48:50

2

你可以试试:

_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; 
} 
+0

此代码给CGContextDrawImage:无效的上下文0x0。这是一个严重的错误。此应用程序或其使用的库正在使用无效的上下文,从而导致系统稳定性和可靠性的整体降级。这个通知是礼貌的:请解决这个问题。这将成为即将到来的更新中的致命错误。 任何想法plz? – user2168496 2015-02-10 12:09:46

+0

检查您的UIImageView实例是否不为空。如果该方法未针对空的UIIMageView调用。 – gabriel 2015-02-10 12:15:33

+0

是有图像,图像显示在图像视图中,然后在用户选择颜色后,我想要更改它的颜色 – user2168496 2015-02-10 12:47:18

4

尝试是这样的

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; 

希望这有助于。

+0

thanx的帮助,但这将图像更改为白色图像(因为它是一个空白的) – user2168496 2015-02-10 12:45:10

+0

所以你说你的_sketchImageView.image是空白的? – Gismay 2015-02-10 13:22:30

+0

不,这不是..我的意思是在应用你的代码后,我得到一个白色的图像 – user2168496 2015-02-10 13:39:50

1

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

16

在斯威夫特你可以使用这个扩展:基于@ VietHung的Objective-C的解决方案]


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 
    } 
} 
0

下面是我如何在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; 
} 
3

在斯威夫特3.0你可以使用这个扩展:

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 
    } 
} 
12

在SWIFT 2上@ VietHung的Objective-C的解决方案基于]。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 
+2

对于Swift 3。0,你会想使用withRenderingMode,而不是imageWithRenderingMode。 – Joe 2017-03-22 03:54:49

1

对于雨燕3.0,我做的UIImageView的自定义子类,称为TintedUIImageView。现在,像使用任何色调的颜色在界面生成器或代码

class TintedUIImageView: UIImageView { 

    override func awakeFromNib() { 
     if let image = self.image { 
      self.image = image.withRenderingMode(.alwaysTemplate) 
     } 
    } 
} 
+0

欢迎来到SO!您的回复看起来像是评论而不是答案。一旦你有足够的[声誉](http://stackoverflow.com/help/whats-reputation),你将可以在任何帖子上[评论](http://stackoverflow.com/help/privileges/comment)。另外检查这[我可以做什么,而不是](https://meta.stackexchange.com/questions/214173/why-do-i-need-50-reputation-to-comment-what-c​​an-i-do-instead )。如果您打算回答,请阅读[如何回答](http://stackoverflow.com/help/how-to-answer)以遵循SO指导原则。 – thewaywewere 2017-05-05 19:55:21

0

一两件事你可以做的,只是你的图像添加到Assets文件夹中的XCode,然后更改渲染模式,模板图像设定,所以每当你改变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-asset-catalog-secrets-you-need-to-know&psig=AFQjCNGnAzVn92pCqM8612o1R0J9q1y7cw&ust=1494619445516498