Esempio n. 1
0
        public Task <IImageSourceServiceResult <UIImage>?> GetImageAsync(IFontImageSource imageSource, float scale = 1, CancellationToken cancellationToken = default)
        {
            if (imageSource.IsEmpty)
            {
                return(FromResult(null));
            }

            try
            {
                // TODO: use a cached way
                var image = RenderImage(imageSource, scale);

                if (image == null)
                {
                    throw new InvalidOperationException("Unable to generate font image.");
                }

                var result = new ImageSourceServiceResult(image, true, () => image.Dispose());

                return(FromResult(result));
            }
            catch (Exception ex)
            {
                Logger?.LogWarning(ex, "Unable to generate font image '{Glyph}'.", imageSource.Glyph);
                throw;
            }
        }
Esempio n. 2
0
        public Task <IImageSourceServiceResult <UIImage>?> GetImageAsync(IFileImageSource imageSource, float scale = 1, CancellationToken cancellationToken = default)
        {
            if (imageSource.IsEmpty)
            {
                return(FromResult(null));
            }

            try
            {
                var image = imageSource.GetPlatformImage();

                if (image == null)
                {
                    throw new InvalidOperationException("Unable to load image file.");
                }

                var result = new ImageSourceServiceResult(image, () => image.Dispose());

                return(FromResult(result));
            }
            catch (Exception ex)
            {
                Logger?.LogWarning(ex, "Unable to load image file '{File}'.", imageSource.File);

                throw;
            }
        }
Esempio n. 3
0
        public async Task <IImageSourceServiceResult <WImageSource>?> GetImageSourceAsync(IFileImageSource imageSource, float scale = 1, CancellationToken cancellationToken = default)
        {
            if (imageSource.IsEmpty)
            {
                return(null);
            }

            var filename = imageSource.File;

            try
            {
                var image = await GetLocal(filename) ?? GetAppPackage(filename);

                if (image == null)
                {
                    throw new InvalidOperationException("Unable to load image file.");
                }

                var result = new ImageSourceServiceResult(image);

                return(result);
            }
            catch (Exception ex)
            {
                Logger?.LogWarning(ex, "Unable to load image file '{File}'.", filename);
                throw;
            }
        }
Esempio n. 4
0
        public async Task <IImageSourceServiceResult <WImageSource>?> GetImageSourceAsync(IStreamImageSource imageSource, float scale = 1, CancellationToken cancellationToken = default)
        {
            if (imageSource.IsEmpty)
            {
                return(null);
            }

            try
            {
                using var stream = await imageSource.GetStreamAsync(cancellationToken);

                if (stream == null)
                {
                    throw new InvalidOperationException("Unable to load image stream.");
                }

                var image = new BitmapImage();

                using var ras = stream.AsRandomAccessStream();
                await image.SetSourceAsync(ras);

                var result = new ImageSourceServiceResult(image);

                return(result);
            }
            catch (Exception ex)
            {
                Logger?.LogWarning(ex, "Unable to load image stream.");
                throw;
            }
        }
        public async Task <IImageSourceServiceResult <Image>?> GetImageAsync(IFontImageSource imageSource, Image image, CancellationToken cancellationToken = default)
        {
            if (imageSource.IsEmpty)
            {
                return(null);
            }

            try
            {
                //TODO : Fix me correctly later.
                var isLoadComplated = await image.LoadAsync(string.Empty, cancellationToken);

                if (!isLoadComplated)
                {
                    throw new InvalidOperationException("Unable to load image file.");
                }

                var result = new ImageSourceServiceResult(image);
                return(result);
            }
            catch (Exception ex)
            {
                Logger?.LogWarning(ex, "Unable to generate font image '{Glyph}'.", imageSource.Glyph);
                throw;
            }
        }
        public async Task <IImageSourceServiceResult <UIImage>?> GetImageAsync(IStreamImageSource imageSource, float scale = 1, CancellationToken cancellationToken = default)
        {
            if (imageSource.IsEmpty)
            {
                return(null);
            }

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

                if (stream == null)
                {
                    throw new InvalidOperationException("Unable to load image stream.");
                }

                using var data = NSData.FromStream(stream);
                var image = UIImage.LoadFromData(data, scale);

                if (image == null)
                {
                    throw new InvalidOperationException("Unable to decode image from stream.");
                }

                var result = new ImageSourceServiceResult(image, () => image.Dispose());

                return(result);
            }
            catch (Exception ex)
            {
                Logger?.LogWarning(ex, "Unable to load image stream.");
                throw;
            }
        }
Esempio n. 7
0
        public async Task <IImageSourceServiceResult <UIImage>?> GetImageAsync(IUriImageSource imageSource, float scale = 1, CancellationToken cancellationToken = default)
        {
            if (imageSource.IsEmpty)
            {
                return(null);
            }

            try
            {
                // TODO: use a real caching library with the URI
                if (imageSource is not IStreamImageSource streamImageSource)
                {
                    return(null);
                }

                using var stream = await streamImageSource.GetStreamAsync(cancellationToken).ConfigureAwait(false);

                if (stream == null)
                {
                    throw new InvalidOperationException($"Unable to load image stream from URI '{imageSource.Uri}'.");
                }

                var data = NSData.FromStream(stream);
                if (data == null)
                {
                    throw new InvalidOperationException("Unable to load image stream data.");
                }

                var image = UIImage.LoadFromData(data, scale);

                if (image == null)
                {
                    throw new InvalidOperationException($"Unable to decode image from URI '{imageSource.Uri}'.");
                }

                var result = new ImageSourceServiceResult(image, () => image.Dispose());

                return(result);
            }
            catch (Exception ex)
            {
                Logger?.LogWarning(ex, "Unable to load image URI '{Uri}'.", imageSource.Uri);
                throw;
            }
        }
        public async Task <IImageSourceServiceResult <Drawable>?> GetDrawableAsync(IFileImageSource imageSource, Context context, CancellationToken cancellationToken = default)
        {
            if (imageSource.IsEmpty)
            {
                return(null);
            }

            var filename = imageSource.File;

            try
            {
                ImageSourceServiceResult?result = null;
                var id = context.GetDrawableId(filename);
                if (id > 0)
                {
                    var drawable = context.GetDrawable(id);
                    if (drawable != null)
                    {
                        result = new ImageSourceServiceResult(drawable);
                    }
                }

                if (result == null)
                {
                    result = await Glide
                             .With(context)
                             .Load(filename, context)
                             .SubmitAsync(context, cancellationToken)
                             .ConfigureAwait(false);
                }

                if (result == null)
                {
                    throw new InvalidOperationException("Unable to load image file.");
                }

                return(result);
            }
            catch (Exception ex)
            {
                Logger?.LogWarning(ex, "Unable to load image file '{File}'.", filename);
                throw;
            }
        }
        public async Task <IImageSourceServiceResult <Image>?> GetImageAsync(IFileImageSource imageSource, Image image, CancellationToken cancellationToken = default)
        {
            if (imageSource.IsEmpty)
            {
                return(null);
            }

            var filename = imageSource.File;

            try
            {
                if (!string.IsNullOrEmpty(filename))
                {
                    var isLoadComplated = await image.LoadAsync(GetPath(filename), cancellationToken);

                    if (!isLoadComplated)
                    {
                        //If it fails, call the Load function to remove the previous image.
                        image.Load(string.Empty);
                        throw new InvalidOperationException("Unable to load image file.");
                    }

                    var result = new ImageSourceServiceResult(image);
                    return(result);
                }
                else
                {
                    throw new InvalidOperationException("Unable to load image file.");
                }
            }
            catch (Exception ex)
            {
                Logger?.LogWarning(ex, "Unable to load image file '{File}'.", filename);
                throw;
            }
        }
Esempio n. 10
0
        public async Task <IImageSourceServiceResult <UIImage>?> GetImageAsync(IUriImageSource imageSource, float scale = 1, CancellationToken cancellationToken = default)
        {
            if (imageSource.IsEmpty)
            {
                return(null);
            }

            try
            {
                var hash             = Crc64.ComputeHashString(imageSource.Uri.OriginalString);
                var pathToImageCache = CacheDirectory + hash + ".png";

                NSData?imageData;

                if (imageSource.CachingEnabled && IsImageCached(pathToImageCache))
                {
                    imageData = GetCachedImage(pathToImageCache);
                }
                else
                {
                    // TODO: use a real caching library with the URI
                    if (imageSource is not IStreamImageSource streamImageSource)
                    {
                        return(null);
                    }

                    using var stream = await streamImageSource.GetStreamAsync(cancellationToken).ConfigureAwait(false);

                    if (stream == null)
                    {
                        throw new InvalidOperationException($"Unable to load image stream from URI '{imageSource.Uri}'.");
                    }

                    imageData = NSData.FromStream(stream);

                    if (imageData == null)
                    {
                        throw new InvalidOperationException("Unable to load image stream data.");
                    }

                    if (imageSource.CachingEnabled)
                    {
                        CacheImage(imageData, pathToImageCache);
                    }
                }

                var image = UIImage.LoadFromData(imageData, scale);

                if (image == null)
                {
                    throw new InvalidOperationException($"Unable to decode image from URI '{imageSource.Uri}'.");
                }

                var result = new ImageSourceServiceResult(image, () => image.Dispose());

                return(result);
            }
            catch (Exception ex)
            {
                Logger?.LogWarning(ex, "Unable to load image URI '{Uri}'.", imageSource.Uri);
                throw;
            }
        }