Xamarin Forms - 图像到字符串和反向 [英] Xamarin Forms - Image to String and reverse
问题描述
我在谷歌上看到了很多帖子,但我无法使其发挥作用。
然而,将 Xamarin.Forms.Image
转换为字符串
似乎很容易和逻辑但是我无法实现它。我尝试从平台渲染器流,仍然无法工作。
I saw so much of post on google and I couldn't make it works.
However, it seems so easy and logic to get an Xamarin.Forms.Image
into a String
but I'm not able to realize it. I tried from stream, from platform renderer, still doesn't work.
我希望它能在每个平台上运行,你能帮助我吗?
I want it to work on every platform, can you help me?
谢谢!
推荐答案
如果你想要的是一个Image的字符串表示,那么首先你需要从此图像中获取 bytes
,然后将其转换为字符串格式,如 Base64
if what you want is a string representation of a Image then first you need to get the bytes
from this image and then convert it into a string format like Base64
但首先我们需要从图像中获取字节,Xamarin.Forms的图像是查看
,其中包含来源
But first we need to get the byte from the image, Xamarin.Forms's Image is a View
which contains a Source
public class Image : View, IImageController, IElementConfiguration<Image>
{
public ImageSource Source { get; set; }
}
该来源用于加载将要显示的图像,我们有某种 ImageSource
( FileImageSource
, StreamImageSource
, UriImageSource
)但是如果我没弄错,目前没办法在Xamarin.Forms中将ImageSource转换为 bytes
,但我们可以使用本机代码
That source is used to load the image that will be shown, we have a some kinds of ImageSource
(FileImageSource
, StreamImageSource
, UriImageSource
) but if I'm not mistaken currently no way to transform ImageSource to bytes
in Xamarin.Forms, but we can use native code for such
在android中我们可以使用 IImageSourceHandler
将ImageSource转换为位图
并将位图
转换为字节
In android we can use IImageSourceHandler
to transform an ImageSource to Bitmap
and form the Bitmap
to bytes
[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,然后从中获取字节
iOS
Same as android we can use IImageSourceHandler to transform into UIImage and then get the bytes from it
[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;
}
}
表格
请注意,我插入了 [assembly:Dependecy(typeof(ImageLoader))]
,因此我们可以使用Xamarin Forms来识别并从每个平台带来正确的ImageLoader所以我们像这样使用它
Forms
Note that I inserted [assembly: Dependecy(typeof(ImageLoader))]
so we can use the Xamarin Forms to recognize and bring the correct ImageLoader from each Platform so we use it like this
byte[] bytes = await DependencyService.Get<IImageLoader>().LoadImageAsync(imgSource);
string base64String = Convert.ToBase64String(bytes) //convert the binnary to a string representation in base64
note
IImageLoader
是一个简单的界面,如下所示
note
IImageLoader
is a simple interface like the following
public interface IImageLoader
{
Task<byte[]> LoadImageAsync(ImageSource source);
}
这篇关于Xamarin Forms - 图像到字符串和反向的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!