Code Snippet: Convertir ImageSource de Xamarin.Forms a una Imagen Nativa
Voy a compartir con ustedes un fragmento de código que suelo usar cuando genero controles personalizados en Xamarin.Forms.
Como un gran fan de las experiencia nativas siempre estoy tratando de tomar ventaja de los tipos que Xamarin.Forms continue para ayudar a los desarrolladores a usar controles extendidos y nuevos componentes usando tipos comunes en el framework, pero algunas veces xamarin tiene algunas implementaciones ocultas que son private o internal, como en este caso el obtener una imagen nativa del tipo ImageSource como puedes observar aquí(iOS):
https://github.com/xamarin/Xamarin.Forms/blob/ae5bbc51230b3a555ceed2ddce4d4fc8aaf8774d/Xamarin.Forms.Platform.iOS/Renderers/ImageElementManager.cs#L279
Así que para este tipo de casos podemos encontrar una buena solución para seguir usando los tipos que provee Xamarin.Forms y poder usarlos en tu código, Les compartiré un fragmento de código que puede re-utlilizar en un proyecto tipo Shared(compartido) o Multi-target y con una simple linea de código serán capaces de obtener la representación nativa de un objeto tipo ImageSource:
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//for iOS UIImage and for Android Bitmap | |
var nativeImage = await myAwesomeImageSource.GetNativeImageAsync(); |
Como puede ver es muy simple de obtener la imagen nativa y con esto puede llegar a realizar implementaciones muy interesantes como la que realice de ejemplo:
Gist:
https://gist.github.com/AlejandroRuiz/ab280fefbdf986ed8c461bd1f1d128ea
Ejemplo Xamarin.Forms:
https://github.com/AlejandroRuiz/XamarinMonthCodeSnippets
Comentarios
Publicar un comentario