【问题标题】:UIImage PNG colorationUIImage PNG 着色
【发布时间】:2017-04-14 09:30:06
【问题描述】:

我想在用户点击 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;
}

【问题讨论】:

    标签: c# ios xamarin uiimage


    【解决方案1】:

    刚刚尝试了您的代码。一切都正确,但您没有选择要填充 PNG 的颜色。

    变化:

    // tint image (losing alpha) - the luminosity of the original image is preserved
    context.SetBlendMode(CGBlendMode.Color);
    context.FillRect(rect);
    

    到:

    // tint image (losing alpha) - the luminosity of the original image is preserved
    context.SetBlendMode(CGBlendMode.Color);
    context.SetFillColor(UIColor.Red.CGColor);
    context.FillRect(rect);
    

    【讨论】:

    • 谢谢,我会试试的,我在原始帖子中看到类似 [tintColor setFill]; 的内容,但我认为这是评论。
    • 不是,原帖没有告诉你tintColor 是一个用颜色设置的变量。要使用它,您必须在上下文中调用SetFillColor。所以要使用tintColor变量,你可以像CGColor tintColor = UIColor.Red.CGColor;一样初始化它,然后调用context.SetFillColor(tintColor);
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-09-25
    • 2014-03-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-07-02
    相关资源
    最近更新 更多