我尝试了不同的方法,但没有找到这个问题的答案。 有没有办法对 XIB 文件中 UIImageView 定义的图像进行着色?
谢谢
最佳答案
这有效
- (UIImage *)imageWithBurnTint:(UIColor *)color { UIImage *img = self; // lets tint the icon - assumes your icons are black UIGraphicsBeginImageContextWithOptions(img.size, NO, 0.0); CGContextRef context = UIGraphicsGetCurrentContext(); CGContextTranslateCTM(context, 0, img.size.height); CGContextScaleCTM(context, 1.0, -1.0); CGRect rect = CGRectMake(0, 0, img.size.width, img.size.height); // draw alpha-mask CGContextSetBlendMode(context, kCGBlendModeNormal); CGContextDrawImage(context, rect, img.CGImage); // draw tint color, preserving alpha values of original image CGContextSetBlendMode(context, kCGBlendModeSourceIn); [color setFill]; CGContextFillRect(context, rect); UIImage *coloredImage = UIGraphicsGetImageFromCurrentImageContext(); UIGraphicsEndImageContext(); return coloredImage; }
然后
[anImageViewInXib setImage:[center.image imageWithBurnTint:[UIColor blackColor]]];
关于ios - XIB 中 UIImage 的色调,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18913336/