【问题标题】:Xamarin Forms - Image to String and reverseXamarin Forms - 图像到字符串和反转
【发布时间】:2023-03-07 16:41:02
【问题描述】:

我在 google 上看到了很多帖子,但我无法使其正常工作。 但是,将Xamarin.Forms.Image 转换为String 似乎很容易和合乎逻辑,但我无法意识到这一点。我尝试了从流,从平台渲染器,仍然无法正常工作。

我希望它适用于所有平台,您能帮帮我吗?

谢谢!

【问题讨论】:

  • 为什么要这样做?您要达到的目标是什么?
  • 因为我的老板想把它存放在一个 mysql 案例中......所以我需要这个字符串中的图像才能保存它。我知道如何从字符串生成图像。但是图像到字符串我找不到它
  • @Emixam23 你想要一个图像的字符串表示吗?
  • 是的.. 然后我可以存储它并将其转换回图像
  • 您需要使用用于创建 Xamarin.Forms.Image 的原始源图像

标签: image xamarin.forms byte


【解决方案1】:

如果您想要的是图像的字符串表示形式,那么首先您需要从该图像中获取bytes,然后将其转换为字符串格式,例如Base64

但首先我们需要从图像中获取字节,Xamarin.Forms 的图像是一个View,其中包含一个Source

public class Image : View, IImageController, IElementConfiguration<Image>
{
    public ImageSource Source { get; set; }
}

该源用于加载将显示的图像,我们有一些ImageSourceFileImageSourceStreamImageSourceUriImageSource)但如果我没记错的话,目前没有办法转换ImageSource 到 Xamarin.Forms 中的bytes,但我们可以使用本机代码来处理此类

安卓

在 Android 中,我们可以使用 IImageSourceHandler 将 ImageSource 转换为 Bitmap 并将 Bitmap 转换为字节

[assembly: Dependency(typeof(ImageLoader))]
public class ImageLoader : IImageLoader
{
    public async Task<byte[]> LoadImageAsync(ImageSource source)
    {
        IImageSourceHandler handler = GetHandlerFor(source);
        var bmp = await handler.LoadImageAsync(source, Forms.Context);
        byte[] result;
        using (Stream ms = new MemoryStream())
        {
            await bmp.CompressAsync(Android.Graphics.Bitmap.CompressFormat.Jpeg, 95, ms);
            result = new byte[ms.Length];
            ms.Position = 0;
            await ms.ReadAsync(result, 0, (int)ms.Length);
        }
        return result;
    }

    private IImageSourceHandler GetHandlerFor(ImageSource source)
    {
        IImageSourceHandler result;
        if (source is FileImageSource) result = new FileImageSourceHandler();
        else if (source is StreamImageSource) result = new StreamImagesourceHandler();
        else result = new ImageLoaderSourceHandler();
        return result;
    }
}

iOS

和Android一样,我们可以使用IImageSourceHandler转换成UIImage,然后从中获取字节

[assembly: Dependency(typeof(ImageLoader))]
public class ImageLoader : IImageLoader
{
    public async Task<byte[]> LoadImageAsync(ImageSource source)
    {
        IImageSourceHandler handler = GetHandlerFor(source);
        UIImage image = await handler.LoadImageAsync(source);
        using (NSData imageData = image.AsPNG())
        {
            return imageData.ToArray();
        }
    }

    private IImageSourceHandler GetHandlerFor(ImageSource source)
    {
        IImageSourceHandler result;
        if (source is FileImageSource) result = new FileImageSourceHandler();
        else if (source is StreamImageSource) result = new StreamImagesourceHandler();
        else result = new ImageLoaderSourceHandler();
        return result;
    }
}

#Forms 请注意,我插入了[assembly: Dependecy(typeof(ImageLoader))],因此我们可以使用 Xamarin 表单从每个平台识别并带来正确的 ImageLoader,因此我们可以像这样使用它

byte[] bytes = await DependencyService.Get<IImageLoader>().LoadImageAsync(imgSource);
string base64String = Convert.ToBase64String(bytes) //convert the binary to a string representation in base64

#注意 IImageLoader是一个简单的界面,如下所示

public interface IImageLoader
{
    Task<byte[]> LoadImageAsync(ImageSource source);
}

【讨论】:

  • android sn-p 卡在var bmp = await handler.LoadImageAsync(source, Forms.Context);,知道为什么吗?
  • Heinst,检查:Task LoadImageAsync (ImageSource imagesource, Context context, CancellationToken cancelationToken = default(CancellationToken)),你应该寻找应用程序输出并确保没有CancelException
猜你喜欢
  • 2018-03-29
  • 2013-05-16
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-06-15
  • 1970-01-01
相关资源
最近更新 更多