如何更改图像tintColor

use*_*496 38 objective-c uiimage uicolor tintcolor ios

我从服务器接收图像,然后根据用户选择的颜色,图像颜色将被更改.

我尝试了以下方法:

_sketchImageView.image = [_sketchImageView.image imageWithRenderingMode:UIImageRenderingModeAlwaysTemplate];
[_sketchImageView setTintColor:color];
Run Code Online (Sandbox Code Playgroud)

我得到了与我的目标相反的东西(UIImage外面的白色用所选颜色着色).

出了什么问题?

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

Ton*_*ony 53

尝试为自己生成新图像

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;
Run Code Online (Sandbox Code Playgroud)

并使用它.

祝好运

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

该解决方案改变所有像素图像的颜色.

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

在此输入图像描述

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

在此输入图像描述

所以:你的形象取决于你的设计方式


Mej*_*idi 20

在Swift中你可以使用这个扩展:[基于@ 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
    }
}
Run Code Online (Sandbox Code Playgroud)


Ruc*_*mal 16

在swift 2.0中你可以使用它

let image = UIImage(named:"your image name")?.imageWithRenderingMode(.AlwaysTemplate)
let yourimageView.tintColor = UIColor.redColor()
yourimageView.image = image
Run Code Online (Sandbox Code Playgroud)

在swift 3.0中你可以使用它

let image = UIImage(named:"your image name")?.withRenderingMode(.alwaysTemplate)
let yourimageView.tintColor = UIColor.red
yourimageView.image = image
Run Code Online (Sandbox Code Playgroud)

  • 对于Swift 3.0,您将需要使用withRenderingMode,而不是imageWithRenderingMode. (2认同)

小智 6

尝试这样的事情

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;
Run Code Online (Sandbox Code Playgroud)

希望这可以帮助.


ode*_*ens 5

在Swift 3.0中,您可以使用以下扩展名:[基于@VietHung的Objective-C解决方案]

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
    }
}
Run Code Online (Sandbox Code Playgroud)