【问题标题】:Tint color is not working for image in Xamarin Forms in iOS?色调颜色不适用于 iOS 中 Xamarin 表单中的图像?
【发布时间】:2021-01-17 01:24:52
【问题描述】:

我正在使用 this nuget 包在 xamarin 表单中处理嵌入式图像色调颜色,在 android 中运行良好,但在 iOS 中运行不正常。 根据我的搜索,我对 AlwaysTemplate 使用了渲染模式,但在我的场景中,我无法使用渲染模式。 我正在做的是使用来自解决方案文件而不是来自 iOS 资产的图像引用。

下面是我的代码 sn-p。

 <controls:TintedImage
           x:Name="ColorChange"
           Aspect="AspectFit"
           HeightRequest="17"
           Source="{local:ImageResource AppName.Images.dot.png}"
           TintColor="{StaticResource PlaceholderColor}"
           VerticalOptions="Center"
           WidthRequest="17" />

                           

如果在 xamarin 表单中没有为 iOS 渲染图像的图像,我想更改 TintColor。


【问题讨论】:

  • 那个包真的过时了,连楼主都存档了。我不知道您是否可以为png 图片着色,也许可以尝试使用 svg 图片。
  • 谢谢!是否可以更改嵌入图像的色调?
  • 任何备用库??

标签: ios image xamarin.forms cross-platform tintcolor


【解决方案1】:

您可以编写Image 的自定义渲染器来更改色调颜色和UIImageRenderingMode

在 Xamarin.forms 中:

public class myImage : Image
{

}

在 iOS 项目中:

[assembly: ExportRenderer(typeof(myImage), typeof(myImageRenderer))]
namespace WorkingWithImages.iOS
{
    public class myImageRenderer : ImageRenderer
    {
        protected override void OnElementChanged(ElementChangedEventArgs<Image> e)
        {
            base.OnElementChanged(e);

            Control.TintColor = UIColor.Red;

        }

        protected override void OnElementPropertyChanged(object sender, PropertyChangedEventArgs e)
        {
            base.OnElementPropertyChanged(sender, e);

            if (Control.Image != null)
            {
                UIImage image = Control.Image;

                image = image.ImageWithRenderingMode(UIImageRenderingMode.AlwaysTemplate);

                Control.Image = image;
            }
        }
    }
}

【讨论】:

  • 谢谢@Jack Hua,我正在使用相同的代码图像渲染模式已更改,但图像色调颜色未更改。 static void showWarning(myImage tintedImage, bool show) { if (show) { if (tintedImage.TintColor != Color.Gray) { tintedImage.Source ="dot.png"; tintedImage.TintColor = 颜色.灰色; } } else { if (tintedImage.TintColor != Color.Green) { tintedImage.Source = "check_circle.png"; tintedImage.TintColor = 颜色.绿色; } } }
  • 你是不是加了断点看代码是否执行了?
  • 是的,它已执行,图像源已更改但 Tintcolor 未更改
  • AlwaysTemplate 下的 tintcolor 会改变吗?可以试试其他渲染模式吗?
  • Hua 谢谢它的工作。附加色调颜色的微小更改' if (Control.Image != null) { Control.Image = Control.Image.ImageWithRenderingMode(UIImageRenderingMode.AlwaysTemplate); Control.TintColor = ((myImage)Element).TintColor.ToUIColor(); }'
猜你喜欢
  • 2018-05-06
  • 1970-01-01
  • 1970-01-01
  • 2018-06-21
  • 2019-11-13
  • 1970-01-01
  • 2014-12-05
  • 2023-03-15
  • 1970-01-01
相关资源
最近更新 更多