Example #1
0
        /// <summary>
        /// Setups the on before image loading.
        /// You can add additional logic here to configure image loader settings before loading
        /// </summary>
        /// <param name="imageLoader">Image loader.</param>
        /// <param name="source">Source.</param>
        /// <param name="loadingPlaceholderSource">Loading placeholder source.</param>
        /// <param name="errorPlaceholderSource">Error placeholder source.</param>
        protected internal virtual void SetupOnBeforeImageLoading(out Work.TaskParameter imageLoader, IImageSourceBinding source, IImageSourceBinding loadingPlaceholderSource, IImageSourceBinding errorPlaceholderSource)
        {
            if (source.ImageSource == Work.ImageSource.Url)
            {
                imageLoader = ImageService.Instance.LoadUrl(source.Path, CacheDuration);
            }
            else if (source.ImageSource == Work.ImageSource.CompiledResource)
            {
                imageLoader = ImageService.Instance.LoadCompiledResource(source.Path);
            }
            else if (source.ImageSource == Work.ImageSource.ApplicationBundle)
            {
                imageLoader = ImageService.Instance.LoadFileFromApplicationBundle(source.Path);
            }
            else if (source.ImageSource == Work.ImageSource.Filepath)
            {
                imageLoader = ImageService.Instance.LoadFile(source.Path);
            }
            else if (source.ImageSource == Work.ImageSource.Stream)
            {
                imageLoader = ImageService.Instance.LoadStream(source.Stream);
            }
            else if (source.ImageSource == Work.ImageSource.EmbeddedResource)
            {
                imageLoader = ImageService.Instance.LoadEmbeddedResource(source.Path);
            }
            else
            {
                imageLoader = null;
                return;
            }

            // CustomKeyFactory
            if (CacheKeyFactory != null)
            {
                var bindingContext = BindingContext;
                imageLoader.CacheKey(CacheKeyFactory.GetKey(Source, bindingContext));
            }

            // LoadingPlaceholder
            if (LoadingPlaceholder != null)
            {
                if (loadingPlaceholderSource != null)
                {
                    imageLoader.LoadingPlaceholder(loadingPlaceholderSource.Path, loadingPlaceholderSource.ImageSource);
                }
            }

            // ErrorPlaceholder
            if (ErrorPlaceholder != null)
            {
                if (errorPlaceholderSource != null)
                {
                    imageLoader.ErrorPlaceholder(errorPlaceholderSource.Path, errorPlaceholderSource.ImageSource);
                }
            }

            // Enable vector image source
            var vect1 = Source as IVectorImageSource;
            var vect2 = LoadingPlaceholder as IVectorImageSource;
            var vect3 = ErrorPlaceholder as IVectorImageSource;

            if (vect1 != null || vect2 != null || vect3 != null)
            {
                int width = (int)((Width > 0 && !double.IsPositiveInfinity(Width)) ? Width
                                  : ((WidthRequest > 0 && !double.IsPositiveInfinity(WidthRequest)) ? WidthRequest : 0));

                int height = (int)((Height > 0 && !double.IsPositiveInfinity(Height)) ? Height
                                   : ((HeightRequest > 0 && !double.IsPositiveInfinity(HeightRequest)) ? HeightRequest : 0));

                if (vect1 != null)
                {
                    if (vect1.VectorWidth > vect1.VectorHeight)
                    {
                        vect1.VectorHeight = 0;
                    }
                    else
                    {
                        vect1.VectorWidth = 0;
                    }

                    imageLoader.WithCustomDataResolver(vect1.GetVectorDataResolver());
                }
                if (vect2 != null)
                {
                    if (vect2.VectorWidth > vect2.VectorHeight)
                    {
                        vect2.VectorHeight = 0;
                    }
                    else
                    {
                        vect2.VectorWidth = 0;
                    }

                    imageLoader.WithCustomLoadingPlaceholderDataResolver(vect2.GetVectorDataResolver());
                }
                if (vect3 != null)
                {
                    if (vect3.VectorWidth > vect3.VectorHeight)
                    {
                        vect3.VectorHeight = 0;
                    }
                    else
                    {
                        vect3.VectorWidth = 0;
                    }

                    imageLoader.WithCustomErrorPlaceholderDataResolver(vect3.GetVectorDataResolver());
                }
            }
            if (CustomDataResolver != null)
            {
                imageLoader.WithCustomDataResolver(CustomDataResolver);
                imageLoader.WithCustomLoadingPlaceholderDataResolver(CustomDataResolver);
                imageLoader.WithCustomErrorPlaceholderDataResolver(CustomDataResolver);
            }

            // Downsample
            if (DownsampleToViewSize && (WidthRequest > 0 || HeightRequest > 0))
            {
                if (HeightRequest > WidthRequest)
                {
                    imageLoader.DownSampleInDip(height: (int)HeightRequest);
                }
                else
                {
                    imageLoader.DownSampleInDip(width: (int)WidthRequest);
                }
            }
            else if (DownsampleToViewSize && (Width > 0 || Height > 0))
            {
                if (Height > Width)
                {
                    imageLoader.DownSampleInDip(height: (int)Height);
                }
                else
                {
                    imageLoader.DownSampleInDip(width: (int)Width);
                }
            }
            else if ((int)DownsampleHeight != 0 || (int)DownsampleWidth != 0)
            {
                if (DownsampleHeight > DownsampleWidth)
                {
                    if (DownsampleUseDipUnits)
                    {
                        imageLoader.DownSampleInDip(height: (int)DownsampleHeight);
                    }
                    else
                    {
                        imageLoader.DownSample(height: (int)DownsampleHeight);
                    }
                }
                else
                {
                    if (DownsampleUseDipUnits)
                    {
                        imageLoader.DownSampleInDip(width: (int)DownsampleWidth);
                    }
                    else
                    {
                        imageLoader.DownSample(width: (int)DownsampleWidth);
                    }
                }
            }
            else if (DownsampleToViewSize)
            {
                // Fallback to a constant value due to a lot people misusing DownsampleToViewSize property
                // More here: https://github.com/luberda-molinet/FFImageLoading/wiki/Xamarin.Forms-API#downsampletoviewsize-bool-default-false
                imageLoader.DownSample(height: 100);

                ImageService.Instance.Config.Logger?.Error("DownsampleToViewSize failed - view is expandable in both dimensions, so it doesn't have a size. Please use DownsampleWidth or DownsampleHeight property.");
            }

            // RetryCount
            if (RetryCount > 0)
            {
                imageLoader.Retry(RetryCount, RetryDelay);
            }

            if (BitmapOptimizations.HasValue)
            {
                imageLoader.BitmapOptimizations(BitmapOptimizations.Value);
            }

            // FadeAnimation
            if (FadeAnimationEnabled.HasValue)
            {
                imageLoader.FadeAnimation(FadeAnimationEnabled.Value, duration: FadeAnimationDuration);
            }

            // FadeAnimationForCachedImages
            if (FadeAnimationEnabled.HasValue && FadeAnimationForCachedImages.HasValue)
            {
                imageLoader.FadeAnimation(FadeAnimationEnabled.Value, FadeAnimationForCachedImages.Value, FadeAnimationDuration);
            }

            // TransformPlaceholders
            if (TransformPlaceholders.HasValue)
            {
                imageLoader.TransformPlaceholders(TransformPlaceholders.Value);
            }

            // Transformations
            if (Transformations != null && Transformations.Count > 0)
            {
                imageLoader.Transform(Transformations);
            }

            if (InvalidateLayoutAfterLoaded.HasValue)
            {
                imageLoader.InvalidateLayout(InvalidateLayoutAfterLoaded.Value);
            }

            imageLoader.WithPriority(LoadingPriority);
            if (CacheType.HasValue)
            {
                imageLoader.WithCache(CacheType.Value);
            }

            if (LoadingDelay.HasValue)
            {
                imageLoader.Delay(LoadingDelay.Value);
            }

            imageLoader.DownloadStarted((downloadInformation) => OnDownloadStarted(new CachedImageEvents.DownloadStartedEventArgs(downloadInformation)));
            imageLoader.DownloadProgress((progress) => OnDownloadProgress(new CachedImageEvents.DownloadProgressEventArgs(progress)));
            imageLoader.FileWriteFinished((fileWriteInfo) => OnFileWriteFinished(new CachedImageEvents.FileWriteFinishedEventArgs(fileWriteInfo)));
            imageLoader.Error((exception) => OnError(new CachedImageEvents.ErrorEventArgs(exception)));
            imageLoader.Finish((work) => OnFinish(new CachedImageEvents.FinishEventArgs(work)));
            imageLoader.Success((imageInformation, loadingResult) => OnSuccess(new CachedImageEvents.SuccessEventArgs(imageInformation, loadingResult)));

            SetupOnBeforeImageLoading(imageLoader);
        }
Example #2
0
        private async void LoadImage()
        {
            if (_currentTask != null)
            {
                _currentTask.Cancel();
            }

            TaskParameter imageLoader = null;

            var ffSource = await FFImageSourceBinding.GetImageSourceBinding(Source).ConfigureAwait(false);

            if (ffSource == null)
            {
                if (internalImage != null)
                {
                    await MainThreadDispatcher.Instance.PostAsync(() => {
                        internalImage.Source = null;
                    });
                }
            }
            else if (ffSource.ImageSource == FFImageLoading.Work.ImageSource.Url)
            {
                imageLoader = ImageService.Instance.LoadUrl(ffSource.Path, TimeSpan.FromDays(CacheDuration));
            }
            else if (ffSource.ImageSource == FFImageLoading.Work.ImageSource.CompiledResource)
            {
                imageLoader = ImageService.Instance.LoadCompiledResource(ffSource.Path);
            }
            else if (ffSource.ImageSource == FFImageLoading.Work.ImageSource.ApplicationBundle)
            {
                imageLoader = ImageService.Instance.LoadFileFromApplicationBundle(ffSource.Path);
            }
            else if (ffSource.ImageSource == FFImageLoading.Work.ImageSource.Filepath)
            {
                imageLoader = ImageService.Instance.LoadFile(ffSource.Path);
            }

            if (imageLoader != null)
            {
                // CustomKeyFactory
                if (CacheKeyFactory != null)
                {
                    var dataContext = DataContext;
                    imageLoader.CacheKey(CacheKeyFactory.GetKey(Source, dataContext));
                }

                // LoadingPlaceholder
                if (LoadingPlaceholder != null)
                {
                    var placeholderSource = await FFImageSourceBinding.GetImageSourceBinding(LoadingPlaceholder);

                    if (placeholderSource != null)
                    {
                        imageLoader.LoadingPlaceholder(placeholderSource.Path, placeholderSource.ImageSource);
                    }
                }

                // ErrorPlaceholder
                if (ErrorPlaceholder != null)
                {
                    var placeholderSource = await FFImageSourceBinding.GetImageSourceBinding(ErrorPlaceholder);

                    if (placeholderSource != null)
                    {
                        imageLoader.ErrorPlaceholder(placeholderSource.Path, placeholderSource.ImageSource);
                    }
                }

                // Downsample
                if (DownsampleToViewSize && (Width > 0 || Height > 0))
                {
                    if (Height > Width)
                    {
                        imageLoader.DownSampleInDip(height: (int)Height);
                    }
                    else
                    {
                        imageLoader.DownSampleInDip(width: (int)Width);
                    }
                }
                else if (DownsampleToViewSize && (MinWidth > 0 || MinHeight > 0))
                {
                    if (MinHeight > MinWidth)
                    {
                        imageLoader.DownSampleInDip(height: (int)MinHeight);
                    }
                    else
                    {
                        imageLoader.DownSampleInDip(width: (int)MinWidth);
                    }
                }
                else if ((int)DownsampleHeight != 0 || (int)DownsampleWidth != 0)
                {
                    if (DownsampleHeight > DownsampleWidth)
                    {
                        if (DownsampleUseDipUnits)
                        {
                            imageLoader.DownSampleInDip(height: (int)DownsampleHeight);
                        }
                        else
                        {
                            imageLoader.DownSample(height: (int)DownsampleHeight);
                        }
                    }
                    else
                    {
                        if (DownsampleUseDipUnits)
                        {
                            imageLoader.DownSampleInDip(width: (int)DownsampleWidth);
                        }
                        else
                        {
                            imageLoader.DownSample(width: (int)DownsampleWidth);
                        }
                    }
                }

                // Downsample mode
                imageLoader.DownSampleMode(DownsampleMode);

                // RetryCount
                if (RetryCount > 0)
                {
                    imageLoader.Retry(RetryCount, RetryDelay);
                }

                // FadeAnimation
                imageLoader.FadeAnimation(FadeAnimationEnabled);

                // TransformPlaceholders
                imageLoader.TransformPlaceholders(TransformPlaceholders);

                // Transformations
                if (Transformations != null && Transformations.Count != 0)
                {
                    imageLoader.Transform(Transformations);
                }

                imageLoader.WithPriority(LoadingPriority);
                imageLoader.WithCache(CacheType);

                imageLoader.Finish((work) =>
                                   OnFinish(new Args.FinishEventArgs(work)));

                imageLoader.Success((imageInformation, loadingResult) =>
                                    OnSuccess(new Args.SuccessEventArgs(imageInformation, loadingResult)));

                imageLoader.Error((exception) =>
                                  OnError(new Args.ErrorEventArgs(exception)));

                _currentTask = imageLoader.Into(internalImage);
            }
        }
Example #3
0
        private async void LoadImage()
        {
            if (_currentTask != null)
            {
                _currentTask.Cancel();
            }

            TaskParameter imageLoader = null;

            var ffSource = await FFImageSourceBinding.GetImageSourceBinding(Source);

            if (ffSource == null)
            {
                if (internalImage != null)
                {
                    await CoreApplication.MainView.CoreWindow.Dispatcher.RunAsync(CoreDispatcherPriority.Normal, () => {
                        internalImage.Source = null;
                    });
                }
            }
            else if (ffSource.ImageSource == FFImageLoading.Work.ImageSource.Url)
            {
                imageLoader = ImageService.LoadUrl(ffSource.Path, TimeSpan.FromDays(CacheDuration));
            }
            else if (ffSource.ImageSource == FFImageLoading.Work.ImageSource.CompiledResource)
            {
                imageLoader = ImageService.LoadCompiledResource(ffSource.Path);
            }
            else if (ffSource.ImageSource == FFImageLoading.Work.ImageSource.ApplicationBundle)
            {
                imageLoader = ImageService.LoadFileFromApplicationBundle(ffSource.Path);
            }
            else if (ffSource.ImageSource == FFImageLoading.Work.ImageSource.Filepath)
            {
                imageLoader = ImageService.LoadFile(ffSource.Path);
            }

            if (imageLoader != null)
            {
                // CustomKeyFactory
                if (CacheKeyFactory != null)
                {
                    var dataContext = DataContext;
                    imageLoader.CacheKey(CacheKeyFactory.GetKey(Source, dataContext));
                }

                // LoadingPlaceholder
                if (LoadingPlaceholder != null)
                {
                    var placeholderSource = await FFImageSourceBinding.GetImageSourceBinding(LoadingPlaceholder);

                    if (placeholderSource != null)
                    {
                        imageLoader.LoadingPlaceholder(placeholderSource.Path, placeholderSource.ImageSource);
                    }
                }

                // ErrorPlaceholder
                if (ErrorPlaceholder != null)
                {
                    var placeholderSource = await FFImageSourceBinding.GetImageSourceBinding(ErrorPlaceholder);

                    if (placeholderSource != null)
                    {
                        imageLoader.ErrorPlaceholder(placeholderSource.Path, placeholderSource.ImageSource);
                    }
                }

                // Downsample
                if (DownsampleToViewSize && (Width > 0 || Height > 0))
                {
                    if (Height > Width)
                    {
                        imageLoader.DownSample(height: Height.PointsToPixels());
                    }
                    else
                    {
                        imageLoader.DownSample(width: Width.PointsToPixels());
                    }
                }
                else if (DownsampleToViewSize && (MinWidth > 0 || MinHeight > 0))
                {
                    if (MinHeight > MinWidth)
                    {
                        imageLoader.DownSample(height: MinHeight.PointsToPixels());
                    }
                    else
                    {
                        imageLoader.DownSample(width: MinWidth.PointsToPixels());
                    }
                }
                else if ((int)DownsampleHeight != 0 || (int)DownsampleWidth != 0)
                {
                    if (DownsampleHeight > DownsampleWidth)
                    {
                        imageLoader.DownSample(height: DownsampleUseDipUnits
                            ? DownsampleHeight.PointsToPixels() : (int)DownsampleHeight);
                    }
                    else
                    {
                        imageLoader.DownSample(width: DownsampleUseDipUnits
                            ? DownsampleWidth.PointsToPixels() : (int)DownsampleWidth);
                    }
                }

                // Downsample mode
                imageLoader.DownSampleMode(DownsampleMode);

                // RetryCount
                if (RetryCount > 0)
                {
                    imageLoader.Retry(RetryCount, RetryDelay);
                }

                // FadeAnimation
                imageLoader.FadeAnimation(FadeAnimationEnabled);

                // TransformPlaceholders
                imageLoader.TransformPlaceholders(TransformPlaceholders);

                // Transformations
                if (Transformations != null && Transformations.Count != 0)
                {
                    imageLoader.Transform(Transformations);
                }

                _currentTask = imageLoader.Into(internalImage);
            }
        }