Skip to content

Instantly share code, notes, and snippets.

@AlejandroRuiz
Created June 2, 2020 03:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save AlejandroRuiz/ab280fefbdf986ed8c461bd1f1d128ea to your computer and use it in GitHub Desktop.
Save AlejandroRuiz/ab280fefbdf986ed8c461bd1f1d128ea to your computer and use it in GitHub Desktop.
Xamarin Month - Code Snippets: Convert Xamarin.Forms ImageSource to Native Image
using System.Threading.Tasks;
using Xamarin.Forms;
#if __IOS__
using Xamarin.Forms.Platform.iOS;
using UIKit;
#elif __ANDROID__
using Android.Graphics;
using Xamarin.Forms.Platform.Android;
using Application = Android.App.Application;
#endif
namespace MyAwesomeExtensions
{
public static class ImageSourceExtensions
{
public static IImageSourceHandler GetDefaultHandler(this ImageSource source)
{
IImageSourceHandler handler = null;
if (source is UriImageSource)
{
handler = new ImageLoaderSourceHandler();
}
else if (source is FileImageSource)
{
handler = new FileImageSourceHandler();
}
else if (source is StreamImageSource)
{
handler = new StreamImagesourceHandler();
}
else if (source is FontImageSource)
{
handler = new FontImageSourceHandler();
}
return handler;
}
public static
#if __IOS__
Task<UIImage>
#elif __ANDROID__
Task<Bitmap>
#endif
GetNativeImageAsync(this ImageSource source)
{
if (source == null)
return null;
var nativeImageHandler = source.GetDefaultHandler();
#if __IOS__
float scale = (float)UIScreen.MainScreen.Scale;
return nativeImageHandler.LoadImageAsync(source, scale: scale);
#elif __ANDROID__
return nativeImageHandler.LoadImageAsync(source, Application.Context);
#endif
}
}
}
//for iOS UIImage and for Android Bitmap
var nativeImage = await myAwesomeImageSource.GetNativeImageAsync();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment