3
我想在用户单击它时着色PNG。UIImage PNG着色
我在Xcode here中找到了一个例子,但是当我在C#中尝试时,我无法检索图像。
这里是我写的代码:
public UIImage DrawSelectedBorder(UIImage image)
{
UIGraphics.BeginImageContextWithOptions(image.Size, false, image.CurrentScale); // for correct resolution on retina, thanks @MobileVet
CGContext context = UIGraphics.GetCurrentContext();
context.TranslateCTM(0, image.Size.Height);
context.ScaleCTM((System.nfloat)1.0, (System.nfloat)(-1.0));
CGRect rect = new CGRect(0, 0, image.Size.Width, image.Size.Height);
context.SetBlendMode(CGBlendMode.Normal);
context.SetFillColor(UIColor.Black.CGColor);
context.FillRect(rect);
// draw original image
context.SetBlendMode(CGBlendMode.Normal);
context.DrawImage(rect,image.CGImage);
// tint image (losing alpha) - the luminosity of the original image is preserved
context.SetBlendMode(CGBlendMode.Color);
context.FillRect(rect);
// mask by alpha values of original image
context.SetBlendMode(CGBlendMode.DestinationIn);
context.DrawImage(rect, image.CGImage);
UIImage coloredImage = UIGraphics.GetImageFromCurrentImageContext();
UIGraphics.EndImageContext();
return coloredImage;
}
谢谢,我会尝试,我看到类似'[tintColor setFill]'在原来的职位,但我想这是一个注释。 – SunLiker67
不是这样,原始文章不会告诉你'tintColor'是一个用颜色设置的变量。要使用它,你必须在上下文中调用SetFillColor。所以要使用'tintColor'变量,你可以像初始化它'CGColor tintColor = UIColor.Red.CGColor;',然后调用'context.SetFillColor(tintColor);' – Leze
非常感谢Leze额外的信息。 – SunLiker67