コード例 #1
0
        internal static async Task <FormsAnimationDrawable> GetFormsAnimationDrawableFromStream(Stream stream, Context context, BitmapFactory.Options options)
        {
            FormsAnimationDrawable animation = null;

            using (var decoder = new AndroidGIFImageParser(context, options.InDensity, options.InTargetDensity))
            {
                try
                {
                    if (!FileImageSourceHandler.DecodeSynchronously)
                    {
                        await decoder.ParseAsync(stream).ConfigureAwait(false);
                    }
                    else
                    {
                        decoder.ParseAsync(stream).Wait();
                    }

                    animation = decoder.Animation;
                }
                catch (GIFDecoderFormatException)
                {
                    animation = null;
                }
            }

            return(animation);
        }
コード例 #2
0
        public static async Task <IFormsAnimationDrawable> LoadImageAnimationAsync(UriImageSource imagesource, Context context, CancellationToken cancelationToken = default(CancellationToken))
        {
            FormsAnimationDrawable animation = null;

            if (imagesource is IStreamImageSource streamImageSource)
            {
                using var stream = await streamImageSource.GetStreamAsync(cancelationToken).ConfigureAwait(false);

                if (stream != null)
                {
                    var options = new BitmapFactory.Options
                    {
                        InJustDecodeBounds = true
                    };

                    using (var decoder = new AndroidGIFImageParser(context, options.InDensity, options.InTargetDensity))
                    {
                        try
                        {
                            if (!FileImageSourceHandler.DecodeSynchronously)
                            {
                                await decoder.ParseAsync(stream).ConfigureAwait(false);
                            }
                            else
                            {
                                decoder.ParseAsync(stream).Wait();
                            }

                            animation = decoder.Animation;
                        }
                        catch (GIFDecoderFormatException ex)
                        {
                            System.Diagnostics.Debug.WriteLine(ex.Message);
                            animation = null;
                        }
                    }

                    if (animation == null)
                    {
                        Application.Current?.FindMauiContext()?.CreateLogger <FileImageSourceHandler>()?.LogWarning("Could not retrieve image or image data was invalid: {imagesource}", imagesource);
                    }
                }
            }

            return(animation);
        }
コード例 #3
0
        public async Task <IFormsAnimationDrawable> LoadImageAnimationAsync(StreamImageSource imagesource, Context context, CancellationToken cancelationToken = default(CancellationToken))
        {
            var streamSource = imagesource as StreamImageSource;
            FormsAnimationDrawable animation = null;

            if (streamSource?.Stream != null)
            {
                using (Stream stream = await((IStreamImageSource)streamSource).GetStreamAsync(cancelationToken).ConfigureAwait(false))
                {
                    int sourceDensity = 1;
                    int targetDensity = 1;

                    if (stream.CanSeek)
                    {
                        BitmapFactory.Options options = new BitmapFactory.Options();
                        options.InJustDecodeBounds = true;
                        await BitmapFactory.DecodeStreamAsync(stream, null, options);

                        sourceDensity = options.InDensity;
                        targetDensity = options.InTargetDensity;
                        stream.Seek(0, SeekOrigin.Begin);
                    }

                    using (var decoder = new AndroidGIFImageParser(context, sourceDensity, targetDensity))
                    {
                        try
                        {
                            await decoder.ParseAsync(stream).ConfigureAwait(false);

                            animation = decoder.Animation;
                        }
                        catch (GIFDecoderFormatException)
                        {
                            animation = null;
                        }
                    }
                }
            }

            if (animation == null)
            {
                Application.Current?.FindMauiContext()?.CreateLogger <ImageLoaderSourceHandler>()?.LogWarning("Image data was invalid: {streamSource}", streamSource);
            }

            return(animation);
        }