UIImage PNG 着色 [英] UIImage PNG coloration

查看:22
本文介绍了UIImage PNG 着色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在用户点击时为 PNG 着色.

I would like to color a PNG when user clicks on it.

我在 Xcode 中找到了一个例子 在这里但是当我在 C# 中尝试时,我无法检索图像.

I found an example in Xcode here but when I try it in C# I can't Retrieve the image.

这是我写的代码:

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;
}

推荐答案

刚刚尝试了您的代码.一切正常,但您没有选择要在 PNG 中填充的颜色.

Just tried your code. Everything is right but you didn't choose the color you want to fill in your 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);

这篇关于UIImage PNG 着色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆