public override Task <IImageSourceServiceResult <Drawable>?> GetDrawableAsync(IImageSource imageSource, Context context, CancellationToken cancellationToken = default)
        {
            var fontImageSource = (IFontImageSource)imageSource;

            if (!fontImageSource.IsEmpty)
            {
                var size     = FontManager.GetFontSize(fontImageSource.Font);
                var unit     = fontImageSource.Font.AutoScalingEnabled ? ComplexUnitType.Sp : ComplexUnitType.Dip;
                var textSize = TypedValue.ApplyDimension(unit, size.Value, context?.Resources?.DisplayMetrics);
                var typeface = FontManager.GetTypeface(fontImageSource.Font);
                var color    = (fontImageSource.Color ?? Graphics.Colors.White).ToPlatform();

                try
                {
                    var drawableCallback = new ImageLoaderResultCallback();

                    PlatformInterop.LoadImageFromFont(
                        context,
                        color,
                        fontImageSource.Glyph,
                        typeface,
                        textSize,
                        drawableCallback);

                    return(drawableCallback.Result);
                }
                catch (Exception ex)
                {
                    Logger?.LogWarning(ex, "Unable to generate font image '{Glyph}'.", fontImageSource.Glyph);
                    throw;
                }
            }
            return(Task.FromResult <IImageSourceServiceResult <Drawable>?>(null));
        }
        public override Task <IImageSourceServiceResult <Drawable>?> GetDrawableAsync(IImageSource imageSource, Context context, CancellationToken cancellationToken = default)
        {
            var fileImageSource = (IFileImageSource)imageSource;

            if (!fileImageSource.IsEmpty)
            {
                try
                {
                    var id = context?.GetDrawableId(fileImageSource.File) ?? -1;
                    if (id > 0)
                    {
                        var d = context?.GetDrawable(id);
                        if (d is not null)
                        {
                            return(Task.FromResult <IImageSourceServiceResult <Drawable>?>(new ImageSourceServiceResult(d)));
                        }
                    }

                    var drawableCallback = new ImageLoaderResultCallback();

                    PlatformInterop.LoadImageFromFile(context, fileImageSource.File, drawableCallback);

                    return(drawableCallback.Result);
                }
                catch (Exception ex)
                {
                    Logger?.LogWarning(ex, "Unable to load image file '{File}'.", fileImageSource.File);
                    throw;
                }
            }

            return(Task.FromResult <IImageSourceServiceResult <Drawable>?>(null));
        }
Beispiel #3
0
        public override async Task <IImageSourceServiceResult <Drawable>?> GetDrawableAsync(IImageSource imageSource, Context context, CancellationToken cancellationToken = default)
        {
            var streamImageSource = (IStreamImageSource)imageSource;

            if (!streamImageSource.IsEmpty)
            {
                Stream?stream = null;

                try
                {
                    stream = await streamImageSource.GetStreamAsync(cancellationToken).ConfigureAwait(false);

                    var drawableCallback = new ImageLoaderResultCallback();

                    PlatformInterop.LoadImageFromStream(context, stream, drawableCallback);

                    var result = await drawableCallback.Result.ConfigureAwait(false);

                    stream?.Dispose();

                    return(result);
                }
                catch (Exception ex)
                {
                    Logger?.LogWarning(ex, "Unable to load image stream.");
                    throw;
                }
                finally
                {
                    if (stream != null)
                    {
                        GC.KeepAlive(stream);
                    }
                }
            }

            return(null);
        }
Beispiel #4
0
        public override Task <IImageSourceServiceResult <Drawable>?> GetDrawableAsync(IImageSource imageSource, Context context, CancellationToken cancellationToken = default)
        {
            var uriImageSource = (IUriImageSource)imageSource;

            if (!uriImageSource.IsEmpty)
            {
                try
                {
                    var drawableCallback = new ImageLoaderResultCallback();

                    PlatformInterop.LoadImageFromUri(context, uriImageSource.Uri.OriginalString, new Java.Lang.Boolean(uriImageSource.CachingEnabled), drawableCallback);

                    return(drawableCallback.Result);
                }
                catch (Exception ex)
                {
                    Logger?.LogWarning(ex, "Unable to load image uri '{Uri}'.", uriImageSource.Uri.OriginalString);
                    throw;
                }
            }

            return(Task.FromResult <IImageSourceServiceResult <Drawable>?>(null));
        }