开发中经常碰到这样婶的情况:一张带alpha通道的图片,有时在某个UI中需要显示成黑色,但在另外一个UI钟要显示白色。
有美工还强点,用ps调一下色OK,但是浪费时间啊。
其实这种小问题,咱自己就能解决。
分享一个改变图片主题色调的方法:
- (UIImage *) imageWithTintColor:(UIColor *)tintColor
{
UIGraphicsBeginImageContextWithOptions(self.size, NO, 0.0f);
[tintColor setFill];
CGRect bounds = CGRectMake(0, 0, self.size.width, self.size.height);
UIRectFill(bounds);
[self drawInRect:bounds blendMode:kCGBlendModeDestinationIn alpha:1.0f];
UIImage *tintedImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return tintedImage;
}
你只需将这段代码复制到UIImage的扩展方法中即可。
当然我们也可以写成对象方法:
- (UIImage *)changeImage:(UIImage *)img ToColor:(UIColor *)tintColor
{
UIGraphicsBeginImageContextWithOptions(img.size, NO, 0.0f);
[tintColor setFill];
CGRect bounds = CGRectMake(0, 0, img.size.width, img.size.height);
UIRectFill(bounds);
[img drawInRect:bounds blendMode:kCGBlendModeDestinationIn alpha:1.0f];
UIImage *tintedImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return tintedImage;
}
赶紧试试吧。
我在AppStroe上传了个APP叫《今日便签》,完美高仿鼎鼎大名的Clear,不仅免费还添加了许多新的功能,使用更爽更简洁,欢迎各位下载使用 : )
本文介绍了一种在iOS开发中改变图片色调的方法,通过自定义UIImage的扩展方法实现,使得图片能在不同UI场景下呈现黑色或白色,提高开发效率。提供了两种实现方式:一种是类别方法,另一种是对象方法。这种方法避免了依赖美工调整,适用于快速调整图片颜色。
8079

被折叠的 条评论
为什么被折叠?



