imageWithRenderingMode 不适用于 setImageWithUrl(对于 tintColor)
imageWithRenderingMode doesn't work with setImageWithUrl (for tintColor)
我可以使用以下方法为 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+ 上测试。
您的代码没有效果,因为您正在分配要从远程 URL 获取的图像,这需要时间,因此 稍后 - 在您的代码具有完成的。因此,这一行什么都不做:
imageView.image = [imageView.image imageWithRenderingMode:UIImageRenderingModeAlwaysTemplate];
...因为在它运行时,imageView.image
仍然是 nil
。
相反,使用带有完成处理程序的网络命令,并且在完成处理程序之前不要更改图像渲染模式。具体来说,我会做的是:首先,下载 图像。现在(在完成处理程序中)导出模板图像并将其分配给图像视图。
编辑:
另一个问题,根据你后来的评论,你似乎不明白图像视图是什么。它包含 一张 图像 - 最近分配的图像。所以您的代码现在的行为完全符合我的预期 - 图像视图显示模板图像,因为这是您分配给它的最后一张图像。
imageWithRenderingMode:UIImageRenderingModeAlwaysTemplate
将原始图像的非透明部分着色为淡色,仅此而已 - 这正是您现在看到的,所以您的代码正在运行。
如果有人仍然遇到这个问题,我的建议是使用 UIButton 而不是 UIImageView。为 UIButton 导入 "UIButton+AFNetworking.h" 和 setUserInteractionEnabled 为 "FALSE",使其表现得像 UIImageView。
这背后的原因是使用系统 UIButton 的 "setTintColor",其功能可能与 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"]];
我可以使用以下方法为 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+ 上测试。
您的代码没有效果,因为您正在分配要从远程 URL 获取的图像,这需要时间,因此 稍后 - 在您的代码具有完成的。因此,这一行什么都不做:
imageView.image = [imageView.image imageWithRenderingMode:UIImageRenderingModeAlwaysTemplate];
...因为在它运行时,imageView.image
仍然是 nil
。
相反,使用带有完成处理程序的网络命令,并且在完成处理程序之前不要更改图像渲染模式。具体来说,我会做的是:首先,下载 图像。现在(在完成处理程序中)导出模板图像并将其分配给图像视图。
编辑:
另一个问题,根据你后来的评论,你似乎不明白图像视图是什么。它包含 一张 图像 - 最近分配的图像。所以您的代码现在的行为完全符合我的预期 - 图像视图显示模板图像,因为这是您分配给它的最后一张图像。
imageWithRenderingMode:UIImageRenderingModeAlwaysTemplate
将原始图像的非透明部分着色为淡色,仅此而已 - 这正是您现在看到的,所以您的代码正在运行。
如果有人仍然遇到这个问题,我的建议是使用 UIButton 而不是 UIImageView。为 UIButton 导入 "UIButton+AFNetworking.h" 和 setUserInteractionEnabled 为 "FALSE",使其表现得像 UIImageView。
这背后的原因是使用系统 UIButton 的 "setTintColor",其功能可能与 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"]];