我可以使用以下方法在UIImageView中为UIImage着色:
imageView = [[UIImageView alloc]initWithImage:[myImage imageWithRenderingMode:UIImageRenderingModeAlwaysTemplate]];
imageView.tintColor = [UIColor redColor];
但是当使用AFNetworking setImageWithUrl
初始化图像时:
imageView = [imageView setImageWithUrl:url];
imageView.image = [imageView.image imageWithRenderingMode:UIImageRenderingModeAlwaysTemplate];
imageView.tintColor = [UIColor redColor];
这没有效果,图像以原始颜色显示。
在iOS 7+上测试过。
答案 0 :(得分:3)
您的代码无效,因为您要分配要从远程URL获取的图像,这需要时间,因此稍后 - 在您的代码完成后。因此,这条线没有任何作用:
imageView.image
...因为,在运行时,nil
仍为imageWithRenderingMode:UIImageRenderingModeAlwaysTemplate
。
相反,使用带有完成处理程序的网络命令,并且不要将图像渲染模式更改为完成处理程序。特别是,我要做的是:首先,下载图像。现在(在完成处理程序中)派生模板图像并将其分配给图像视图。
修改强>:
根据您之后的评论,另一个问题是,您似乎无法理解图像视图是什么。它包含一个图像 - 最近分配的图像。因此,您的代码现在的行为与我期望的完全一样 - 图像视图显示模板图像,因为这是您分配给它的最后一个图像。
{{1}}将原始图像的非透明部分着色为色彩和所有颜色 - 这正是您现在看到的,因此您的代码正在运行。
答案 1 :(得分:0)
如果有人仍然遇到这个问题,我的建议是使用UIButton而不是UIImageView。导入" UIButton + AFNetworking.h"和setUserInteractionEnabled为" FALSE"对于UIButton,使其行为类似于UIImageView。
这背后的原因是使用" setTintColor"系统UIButton的功能可能与imageWithRenderingMode相同:UIImageRenderingModeAlwaysTemplate
检查以下代码:
UIButton *myButton = [UIButton buttonWithType:UIButtonTypeSystem]; // to use tint color property
[myButton setFrame:CGRectMake(xPos, yPos, btnWidth, btnHeight)];
[myButton setTintColor:[UIColor lightGrayColor]];
[myButton setImageForState:UIControlStateNormal withURL:imageURL placeholderImage:[UIImage imageNamed:@"placeholder.png"]];