前一段時(shí)間接到一個(gè)需求,說我們的應(yīng)用需要換一整套皮膚,圖片的形狀大小尺寸甚至命名都不用修改,無疑這個(gè)時(shí)候UI設(shè)計(jì)師和我們的工作顯得有些重復(fù)而笨重蹂午。
無意間找到了一個(gè)用代碼修改圖片顏色,代碼無痛接入,簡(jiǎn)直酸爽!
@interface UIImage (TintCorlor)
///修改圖片顏色
- (UIImage *) imageWithTintColor:(UIColor *)tintColor;
///修改圖片顏色 同時(shí)保留透明度
- (UIImage *) imageWithGradientTintColor:(UIColor *)tintColor;
@end
- (UIImage *) imageWithTintColor:(UIColor *)tintColor{
return [self imageWithTintColor:tintColor blendMode:kCGBlendModeDestinationIn];
}
- (UIImage *) imageWithGradientTintColor:(UIColor *)tintColor{
return [self imageWithTintColor:tintColor blendMode:kCGBlendModeOverlay];
}
- (UIImage *) imageWithTintColor:(UIColor *)tintColor blendMode:(CGBlendMode)blendMode{
//We want to keep alpha, set opaque to NO; Use 0.0f for scale to use the scale factor of the device’s main screen.
UIGraphicsBeginImageContextWithOptions(self.size, NO, 0.0f);
[tintColor setFill];
CGRect bounds = CGRectMake(0, 0, self.size.width, self.size.height);
UIRectFill(bounds);
//Draw the tinted image in context
[self drawInRect:bounds blendMode:blendMode alpha:1.0f];
if (blendMode != kCGBlendModeDestinationIn) {
[self drawInRect:bounds blendMode:kCGBlendModeDestinationIn alpha:1.0f];
}
UIImage *tintedImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return tintedImage;
}