我有一个返回颜色替换图像的公共静态方法(我传入一个黑色图像,它返回相同的图像,红色而不是黑色,或者我选择的任何颜色)。
它看起来像这样:
public class ColorImage
{
public static UIImage GetColoredImage(UIImage image, UIColor color)
{
UIImage coloredImage = null;
if (color == null)
{
color = UIColor.FromRGB(44, 132, 248);
}
UIGraphics.BeginImageContextWithOptions(image.Size, false, UIScreen.MainScreen.Scale);
using (CGContext context = UIGraphics.GetCurrentContext())
{
context.TranslateCTM(0, image.Size.Height);
context.ScaleCTM(1.0f, -1.0f);
var rect = new RectangleF(0, 0, (float)image.Size.Width, (float)image.Size.Height);
context.ClipToMask(rect, image.CGImage);
context.SetFillColor(color.CGColor);
context.FillRect(rect);
coloredImage = UIGraphics.GetImageFromCurrentImageContext();
UIGraphics.EndImageContext();
}
return coloredImage;
}
}
我正在与一位同事交谈,我们认为存储图像(颜色而不是颜色)以及我们计划在公共静态字段中的整个应用程序中使用的颜色可能会更好。这样,如果我需要使用其中一种颜色或图像,我可以调用:
Textfield.BackgroundColor = ColorCache.MainColor;
Textfield.Image = ImageCache.UserImage;
或类似的规定。这适用于使用 Xamarin 的 iOS 应用程序。这样做有问题吗?认为它甚至可能使我们的一些列表视图加载速度更快,因为它们没有抓取图像并一遍又一遍地处理它们的颜色。