Forum Xamarin.iOS

Announcement:

The Xamarin Forums have officially moved to the new Microsoft Q&A experience. Microsoft Q&A is the home for technical questions and answers at across all products at Microsoft now including Xamarin!

To create new threads and ask questions head over to Microsoft Q&A for .NET and get involved today.

How to change a color of a Image in Xamarin.IOS

I have an Image White and I would like to change the color for other is it possible?

Best Answer

  • amadeuantunesamadeuantunes Member ✭✭
    Accepted Answer

    Like the Image only has one color what I did was take off the image and then just set background color and It worked

    ///UIImageView ImagemFundo
    ImagemFundo.Image = null;
    ImagemFundo.BackgroundColor = UIColor.Black;

Answers

  • DavidCarawayDavidCaraway USBeta ✭✭

    I can't recall where I found this code but it has worked well for me. I only use it to theme a small amount of images so I've never needed to look at its performance.

    public static UIImage GetColoredImage(string imageName, UIColor color)
    {
    UIImage image = UIImage.FromBundle(imageName);
    UIImage imageFromCurrentImageContext = null;
    UIGraphics.BeginImageContextWithOptions(image.Size, false, UIScreen.MainScreen.Scale);
    using (CGContext context = UIGraphics.GetCurrentContext())
    {
    context.TranslateCTM(0, image.Size.Height);
    context.ScaleCTM(1f, -1f);
    RectangleF rect = new RectangleF(0f, 0f, (float) image.Size.Width, (float) image.Size.Height);
    context.SetBlendMode(CGBlendMode.Normal);
    context.DrawImage(rect, image.CGImage);
    context.SetBlendMode(CGBlendMode.SourceIn);
    context.SetFillColor(color.CGColor);
    context.FillRect(rect);
    imageFromCurrentImageContext = UIGraphics.GetImageFromCurrentImageContext();
    UIGraphics.EndImageContext();
    }
    return imageFromCurrentImageContext;
    }
    }

  • amadeuantunesamadeuantunes Member ✭✭
    Accepted Answer

    Like the Image only has one color what I did was take off the image and then just set background color and It worked

    ///UIImageView ImagemFundo
    ImagemFundo.Image = null;
    ImagemFundo.BackgroundColor = UIColor.Black;

Sign In or Register to comment.