Hello,
There is a known issue reported at GitHub - Need a way to convert an ImageSource into an IImage. · Issue #10624 (github.com), please follow the progress. This issue doesn't mention FontImage
, you could add comments at GitHub or create a new feature request.
As I understood, you want to convert the image to Stream, then create a new IImage
. After the image is loaded on the page, the handler won't be null. And you can get the handler in the Loaded
method, then get the stream and call canvas.DrawImage
method. Please refer to the following code on iOS platform:
XAML
<VerticalStackLayout>
<Label
Text="Welcome to .NET MAUI!"
VerticalOptions="Center"
HorizontalOptions="Center" />
<Image x:Name="MyFontImage" BackgroundColor="#D1D1D1">
<Image.Source>
<FontImageSource Glyph=""
FontFamily="{OnPlatform iOS=Ionicons, Android=ionicons.ttf#}"
Size="44" />
</Image.Source>
</Image>
<GraphicsView x:Name="MyGraphyView" BackgroundColor="#D1D1D1"
HeightRequest="200"
WidthRequest="400" />
</VerticalStackLayout>
Loaded
method:
public NewPage1()
{
InitializeComponent();
this.Loaded += NewPage1_Loaded;
}
Setting Drawable
private void NewPage1_Loaded(object sender, EventArgs e)
{
MyGraphyView.Drawable = new ImagePaintDrawable(MyFontImage);
}
Drawable
internal class ImagePaintDrawable : IDrawable
{
public Image MyFontImage { get; set; } = new Image();
public ImagePaintDrawable(Image image)
{
MyFontImage = image;// pass the image
}
public void Draw(ICanvas canvas, RectF dirtyRect)
{
#if IOS || ANDROID || MACCATALYST
IImage image;
#if IOS
UIKit.UIImageView uIImage = MyFontImage.Handler.PlatformView as UIKit.UIImageView;// handler won't be null
var A = uIImage.Image;
Stream imageStream = A.AsPNG().AsStream();
image = PlatformImage.FromStream(imageStream);//get stream (iOS platform)
if (image != null)
{
canvas.DrawImage(image, 10, 10, image.Width, image.Height);// draw
}
#endif
#endif
}
}
Best Regards,
Wenyan Zhang
If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.