11

例如,在iOS工具栏中,你拖入你自己的.png,它是不透明的黑色和透明的,它会自动添加iOS蓝色光泽。

但是,我想知道自己如何做到这一点,但只有纯色就可以了。

例如,如果您有此图像: 例子

你会怎么做才能使整个固体,粉红色,蓝色或灰色?我想通过使用代码对它们进行着色来减少我在应用程序中保存的 .png 版本的数量。

谢谢!

4

2 回答 2

15

这应该为你做:

- (UIImage *)colorImage:(UIImage *)origImage withColor:(UIColor *)color
{
    UIGraphicsBeginImageContextWithOptions(origImage.size, YES, 0);
    CGContextRef context = UIGraphicsGetCurrentContext();

    CGContextSetFillColorWithColor(context, [color CGColor]);
    CGContextFillRect(context, (CGRect){ {0,0}, origImage.size} );

    CGAffineTransform flipVertical = CGAffineTransformMake(1, 0, 0, -1, 0, origImage.size.height);
    CGContextConcatCTM(context, flipVertical);
    CGContextDrawImage(context, (CGRect){ pt, origImage.size }, [origImage CGImage]);

    UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();

    return image;
}
于 2012-08-18T15:09:20.613 回答
1

从 iOS 7 开始,您现在可以像这样用单一颜色为黑色(灰度)/透明图像着色

UIImage *image = [UIImage imageNamed:@"myImage.png"];
UIImageView *imageView = [[UIImageView alloc]initWithImage:[image imageWithRenderingMode:UIImageRenderingModeAlwaysTemplate]];
imageView.tintColor = [UIColor redColor];
于 2014-12-08T11:25:38.790 回答