Xamarin Forms - Image to String and reverse

青春壹個敷衍的年華 提交于 2019-12-24 08:15:06

问题


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?

Thank !


回答1:


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

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; }
}

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

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

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;
    }
}

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

IImageLoaderis a simple interface like the following

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


来源:https://stackoverflow.com/questions/41741464/xamarin-forms-image-to-string-and-reverse

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!