Пример #1
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;
            }
        }
        public async Task <IImageSourceServiceResult <Drawable>?> GetDrawableAsync(IFileImageSource imageSource, Context context, CancellationToken cancellationToken = default)
        {
            if (imageSource.IsEmpty)
            {
                return(null);
            }

            var filename = imageSource.File;

            try
            {
                var 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;
            }
        }
Пример #3
0
        internal static UIImage?GetPlatformImage(this IFileImageSource imageSource)
        {
            var filename = imageSource.File;

            return(File.Exists(filename)
                                                ? UIImage.FromFile(filename)
                                                : UIImage.FromBundle(filename));
        }
Пример #4
0
        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;
            }
        }
Пример #5
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;
            }
        }