如何在iOS中对黑色透明图像进行颜色填充?

Qim*_*ing 11 iphone colors objective-c sprite ios

例如,在iOS工具栏中,您可以拖动自己的.png,它是不透明的黑色和透明,并自动添加iOS蓝色光泽.

但是,我想知道如何自己做,但只有纯色才能做到.

例如,如果你有这个图像: 例

你会做什么来制作整个实心,粉红色,蓝色或灰色?我想通过使用代码着色来减少我在应用程序中保存的.png版本的数量.

谢谢!

Dav*_*d H 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;
}
Run Code Online (Sandbox Code Playgroud)