ios - 如何更改图像 tintColor

标签 ios objective-c uiimage uicolor tintcolor

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

我尝试了以下方法:

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

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

出了什么问题?

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

最佳答案

尝试为自己生成新图像

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;

然后使用它。

祝你好运

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

此解决方案将改变所有像素图像的颜色。

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

enter image description here

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

enter image description here

SO:您的图像如何取决于您的设计方式

关于ios - 如何更改图像 tintColor,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28427935/

相关文章:

javascript - 预期行尾等,但发现未知标记。- AppleScript 编辑器

iphone - int++ 递增 4

objective-c - Cocoa/Objective-C NSButton 事件处理?

ios - UIImage imageNamed 需要加@3x 后缀吗?

ios - iOS 中的 Swift : Resizing UIImage for Portrait or Landscape

ios - NSURL 已停止在应用程序中工作

iphone - 使用 "User Defined Runtime Attributes"在 Storyboard中本地化 View

ios - WatchKit Long Look 通知未显示

objective-c - 如何使用数组作为函数参数

ios - [UICollectionViewCell cellImageView] : unrecognized selector sent to instance