コード例 #1
0
        protected override void SetupOnBeforeImageLoading(TaskParameter imageLoader)
        {
            base.SetupOnBeforeImageLoading(imageLoader);

#if __IOS__
            int width  = (int)this.Bounds.Width;
            int height = (int)this.Bounds.Height;
#elif __ANDROID__
            int width  = this.Width;
            int height = this.Height;
#elif __WINDOWS__
            int width  = (int)this.Width;
            int height = (int)this.Height;
#endif

            if ((!string.IsNullOrWhiteSpace(ImagePath) && ImagePath.IsSvgFileUrl()) || ImageStream != null)
            {
                imageLoader.WithCustomDataResolver(new SvgDataResolver(width, height, true, ReplaceStringMap));
            }
            if (!string.IsNullOrWhiteSpace(LoadingPlaceholderImagePath) && LoadingPlaceholderImagePath.IsSvgFileUrl())
            {
                imageLoader.WithCustomLoadingPlaceholderDataResolver(new SvgDataResolver(width, height, true, ReplaceStringMap));
            }
            if (!string.IsNullOrWhiteSpace(ErrorPlaceholderImagePath) && ErrorPlaceholderImagePath.IsSvgFileUrl())
            {
                imageLoader.WithCustomErrorPlaceholderDataResolver(new SvgDataResolver(width, height, true, ReplaceStringMap));
            }
        }
コード例 #2
0
        private void SetSvgImage(TaskParameter loader)
        {
            ImageViewAsync imageAsync;

            if ((imageAsync = CastImageViewToAsync()) == null)
            {
                return;
            }

            loader
            .WithCustomDataResolver(new SvgDataResolver(200))
            .WithCustomLoadingPlaceholderDataResolver(new SvgDataResolver(200))
            .Finish(obj => SetImageColorFilter(_imageColor))
            .Into(imageAsync);
        }
コード例 #3
0
        public static void SetImage(this UIImageView current, string url, ImageLoadType type = ImageLoadType.Xamarin,
                                    Action success          = null, Action <Exception> error = null, Action startLoading = null,
                                    string imagePlaceholder = "error_placeholder.png", bool showLoadIndicator = true,
                                    UIActivityIndicatorViewStyle loadIndicatorType = UIActivityIndicatorViewStyle.Gray)
        {
            if (url.IsNullOrWhiteSpace())
            {
                AppTools.InvokeOnMainThread(() =>
                {
                    current.Image = null;
                });

                return;
            }

            var activityIndicator = PrepareImageView(current, showLoadIndicator, loadIndicatorType);

            TaskParameter loader = null;

            if (!url.Contains("http"))
            {
                loader = ImageService.Instance.LoadCompiledResource(url);
            }
            else
            {
                loader = ImageService.Instance.LoadUrl(url);
            }

            if (url.Contains(".svg"))
            {
                loader
                .WithCustomDataResolver(new SvgDataResolver(200, 0, true))
                .WithCustomLoadingPlaceholderDataResolver(new SvgDataResolver(200, 0, true));
            }

            RunImageLoader(current, success, error, startLoading, imagePlaceholder, activityIndicator, loader);
        }
コード例 #4
0
        protected virtual void UpdateImageLoadingTask()
        {
            var ffSource          = GetImageSourceBinding(ImagePath, ImageStream);
            var placeholderSource = GetImageSourceBinding(LoadingPlaceholderImagePath, null);

            Cancel();
            TaskParameter imageLoader = null;

            if (ffSource == null)
            {
                _internalImage.Source = null;
                IsLoading             = false;
                return;
            }

            IsLoading = true;

            if (ffSource.ImageSource == FFImageLoading.Work.ImageSource.Url)
            {
                imageLoader = ImageService.Instance.LoadUrl(ffSource.Path, 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);
            }
            else if (ffSource.ImageSource == FFImageLoading.Work.ImageSource.Stream)
            {
                imageLoader = ImageService.Instance.LoadStream(ffSource.Stream);
            }
            else if (ffSource.ImageSource == FFImageLoading.Work.ImageSource.EmbeddedResource)
            {
                imageLoader = ImageService.Instance.LoadEmbeddedResource(ffSource.Path);
            }

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

                // ErrorPlaceholder
                if (!string.IsNullOrWhiteSpace(ErrorPlaceholderImagePath))
                {
                    var errorPlaceholderSource = GetImageSourceBinding(ErrorPlaceholderImagePath, null);
                    if (errorPlaceholderSource != null)
                    {
                        imageLoader.ErrorPlaceholder(errorPlaceholderSource.Path, errorPlaceholderSource.ImageSource);
                    }
                }

                if (CustomDataResolver != null)
                {
                    imageLoader.WithCustomDataResolver(CustomDataResolver);
                    imageLoader.WithCustomLoadingPlaceholderDataResolver(CustomLoadingPlaceholderDataResolver);
                    imageLoader.WithCustomErrorPlaceholderDataResolver(CustomErrorPlaceholderDataResolver);
                }

                // Downsample
                if ((int)DownsampleHeight != 0 || (int)DownsampleWidth != 0)
                {
                    if (DownsampleUseDipUnits)
                    {
                        imageLoader.DownSampleInDip((int)DownsampleWidth, (int)DownsampleHeight);
                    }
                    else
                    {
                        imageLoader.DownSample((int)DownsampleWidth, (int)DownsampleHeight);
                    }
                }

                // 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 > 0)
                {
                    imageLoader.Delay(LoadingDelay);
                }

                imageLoader.Finish((work) =>
                {
                    IsLoading = false;
                    OnFinish?.Invoke(this, new Args.FinishEventArgs(work));
                });

                imageLoader.Success((imageInformation, loadingResult) =>
                {
                    OnSuccess?.Invoke(this, new Args.SuccessEventArgs(imageInformation, loadingResult));
                    _lastImageSource = ffSource;
                });

                if (OnError != null)
                {
                    imageLoader.Error((ex) => OnError?.Invoke(this, new Args.ErrorEventArgs(ex)));
                }

                if (OnDownloadStarted != null)
                {
                    imageLoader.DownloadStarted((downloadInformation) => OnDownloadStarted(this, new Args.DownloadStartedEventArgs(downloadInformation)));
                }

                if (OnDownloadProgress != null)
                {
                    imageLoader.DownloadProgress((progress) => OnDownloadProgress(this, new Args.DownloadProgressEventArgs(progress)));
                }

                if (OnFileWriteFinished != null)
                {
                    imageLoader.FileWriteFinished((info) => OnFileWriteFinished(this, new Args.FileWriteFinishedEventArgs(info)));
                }

                if (!string.IsNullOrWhiteSpace(CustomCacheKey))
                {
                    imageLoader.CacheKey(CustomCacheKey);
                }

                SetupOnBeforeImageLoading(imageLoader);

                _scheduledWork = imageLoader.Into(_internalImage);
            }
        }
コード例 #5
0
        private async void UpdateSource()
        {
            ((Xamarin.Forms.IElementController)Element).SetValueFromRenderer(CachedImage.IsLoadingPropertyKey, true);

            Xamarin.Forms.ImageSource source = Element.Source;

            Cancel();
            TaskParameter imageLoader = null;

            var ffSource = await ImageSourceBinding.GetImageSourceBinding(source, Element).ConfigureAwait(false);

            if (ffSource == null)
            {
                if (Control != null)
                {
                    Control.Source = null;
                }

                ImageLoadingFinished(Element);
            }
            else if (ffSource.ImageSource == FFImageLoading.Work.ImageSource.Url)
            {
                imageLoader = ImageService.Instance.LoadUrl(ffSource.Path, Element.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);
            }
            else if (ffSource.ImageSource == FFImageLoading.Work.ImageSource.Stream)
            {
                imageLoader = ImageService.Instance.LoadStream(ffSource.Stream);
            }

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

                // LoadingPlaceholder
                if (Element.LoadingPlaceholder != null)
                {
                    var placeholderSource = await ImageSourceBinding.GetImageSourceBinding(Element.LoadingPlaceholder, Element).ConfigureAwait(false);

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

                // ErrorPlaceholder
                if (Element.ErrorPlaceholder != null)
                {
                    var placeholderSource = await ImageSourceBinding.GetImageSourceBinding(Element.ErrorPlaceholder, Element).ConfigureAwait(false);

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

                // Enable vector image source
                var vect1 = Element.Source as IVectorImageSource;
                var vect2 = Element.LoadingPlaceholder as IVectorImageSource;
                var vect3 = Element.ErrorPlaceholder as IVectorImageSource;
                if (vect1 != null)
                {
                    imageLoader.WithCustomDataResolver(vect1.GetVectorDataResolver());
                }
                if (vect2 != null)
                {
                    imageLoader.WithCustomLoadingPlaceholderDataResolver(vect2.GetVectorDataResolver());
                }
                if (vect3 != null)
                {
                    imageLoader.WithCustomErrorPlaceholderDataResolver(vect3.GetVectorDataResolver());
                }
                if (Element.CustomDataResolver != null)
                {
                    imageLoader.WithCustomDataResolver(Element.CustomDataResolver);
                    imageLoader.WithCustomLoadingPlaceholderDataResolver(Element.CustomDataResolver);
                    imageLoader.WithCustomErrorPlaceholderDataResolver(Element.CustomDataResolver);
                }

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

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

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

                // FadeAnimation
                if (Element.FadeAnimationEnabled.HasValue)
                {
                    imageLoader.FadeAnimation(Element.FadeAnimationEnabled.Value);
                }

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

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

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

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

                var element = Element;

                imageLoader.Finish((work) =>
                {
                    element.OnFinish(new CachedImageEvents.FinishEventArgs(work));
                    ImageLoadingFinished(element);
                });

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

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

                imageLoader.DownloadStarted((downloadInformation) =>
                                            element.OnDownloadStarted(new CachedImageEvents.DownloadStartedEventArgs(downloadInformation)));

                _currentTask = imageLoader.Into(Control);
            }
        }
コード例 #6
0
        private void UpdateBitmap(CachedImage previous = null)
        {
            Xamarin.Forms.ImageSource source = Element.Source;

            var imageView = Control;

            var ffSource          = ImageSourceBinding.GetImageSourceBinding(source, Element);
            var placeholderSource = ImageSourceBinding.GetImageSourceBinding(Element.LoadingPlaceholder, Element);

            if (previous != null && _lastImageSource != null && ffSource != null && !ffSource.Equals(_lastImageSource) &&
                (string.IsNullOrWhiteSpace(placeholderSource?.Path) || placeholderSource?.Stream != null))
            {
                _lastImageSource = null;

                if (imageView != null)
                {
                    imageView.SkipInvalidate();
                }

                Control.SetImageResource(global::Android.Resource.Color.Transparent);
            }

            Element.SetIsLoading(true);

            if (Element != null && object.Equals(Element.Source, source) && !_isDisposed)
            {
                Cancel();
                TaskParameter imageLoader = null;

                if (ffSource == null)
                {
                    //if (imageView != null)
                    imageView.SetImageResource(global::Android.Resource.Color.Transparent);

                    ImageLoadingFinished(Element);
                }
                else if (ffSource.ImageSource == FFImageLoading.Work.ImageSource.Url)
                {
                    imageLoader = ImageService.Instance.LoadUrl(ffSource.Path, Element.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);
                }
                else if (ffSource.ImageSource == FFImageLoading.Work.ImageSource.Stream)
                {
                    imageLoader = ImageService.Instance.LoadStream(ffSource.Stream);
                }
                else if (ffSource.ImageSource == FFImageLoading.Work.ImageSource.EmbeddedResource)
                {
                    imageLoader = ImageService.Instance.LoadEmbeddedResource(ffSource.Path);
                }

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

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

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

                    // Enable vector image source
                    var vect1 = Element.Source as IVectorImageSource;
                    var vect2 = Element.LoadingPlaceholder as IVectorImageSource;
                    var vect3 = Element.ErrorPlaceholder as IVectorImageSource;
                    if (vect1 != null)
                    {
                        imageLoader.WithCustomDataResolver(vect1.GetVectorDataResolver());
                    }
                    if (vect2 != null)
                    {
                        imageLoader.WithCustomLoadingPlaceholderDataResolver(vect2.GetVectorDataResolver());
                    }
                    if (vect3 != null)
                    {
                        imageLoader.WithCustomErrorPlaceholderDataResolver(vect3.GetVectorDataResolver());
                    }
                    if (Element.CustomDataResolver != null)
                    {
                        imageLoader.WithCustomDataResolver(Element.CustomDataResolver);
                        imageLoader.WithCustomLoadingPlaceholderDataResolver(Element.CustomDataResolver);
                        imageLoader.WithCustomErrorPlaceholderDataResolver(Element.CustomDataResolver);
                    }

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

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

                    // TransparencyChannel
                    if (Element.TransparencyEnabled.HasValue)
                    {
                        imageLoader.TransparencyChannel(Element.TransparencyEnabled.Value);
                    }

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

                    // FadeAnimation
                    if (Element.FadeAnimationEnabled.HasValue)
                    {
                        imageLoader.FadeAnimation(Element.FadeAnimationEnabled.Value);
                    }

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

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

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

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

                    var element = Element;

                    imageLoader.Finish((work) =>
                    {
                        element.OnFinish(new CachedImageEvents.FinishEventArgs(work));
                        ImageLoadingFinished(element);
                    });

                    imageLoader.Success((imageInformation, loadingResult) =>
                    {
                        element.OnSuccess(new CachedImageEvents.SuccessEventArgs(imageInformation, loadingResult));
                        _lastImageSource = ffSource;
                    });

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

                    imageLoader.DownloadStarted((downloadInformation) =>
                                                element.OnDownloadStarted(new CachedImageEvents.DownloadStartedEventArgs(downloadInformation)));

                    imageLoader.DownloadProgress((progress) =>
                                                 element.OnDownloadProgress(new CachedImageEvents.DownloadProgressEventArgs(progress)));

                    imageLoader.FileWriteFinished((fileWriteInfo) =>
                                                  element.OnFileWriteFinished(new CachedImageEvents.FileWriteFinishedEventArgs(fileWriteInfo)));

                    element.SetupOnBeforeImageLoading(imageLoader);

                    _currentTask = imageLoader.Into(imageView);
                }
            }
        }
コード例 #7
0
        private void SetImage(CachedImage oldElement = null)
        {
            Xamarin.Forms.ImageSource source = null;
            var vectorSource = Element.Source as IVectorImageSource;

            if (vectorSource != null)
            {
                source = vectorSource.ImageSource;
            }
            else
            {
                source = Element.Source;
            }

            var ffSource          = ImageSourceBinding.GetImageSourceBinding(source);
            var placeholderSource = ImageSourceBinding.GetImageSourceBinding(Element.LoadingPlaceholder);

            if (oldElement != null && _lastImageSource != null && ffSource != null && !ffSource.Equals(_lastImageSource) &&
                (string.IsNullOrWhiteSpace(placeholderSource?.Path) || placeholderSource?.Stream != null))
            {
                _lastImageSource = null;
                Control.Image    = null;
            }

            ((IElementController)Element).SetValueFromRenderer(CachedImage.IsLoadingPropertyKey, true);

            Cancel();
            TaskParameter imageLoader = null;

            if (ffSource == null)
            {
                if (Control != null)
                {
                    Control.Image = null;
                }

                ImageLoadingFinished(Element);
            }
            else if (ffSource.ImageSource == FFImageLoading.Work.ImageSource.Url)
            {
                imageLoader = ImageService.Instance.LoadUrl(ffSource.Path, Element.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);
            }
            else if (ffSource.ImageSource == FFImageLoading.Work.ImageSource.Stream)
            {
                imageLoader = ImageService.Instance.LoadStream(ffSource.Stream);
            }

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

                // CustomDataResolver
                if (Element.CustomDataResolver != null)
                {
                    imageLoader.WithCustomDataResolver(Element.CustomDataResolver);
                }
                else if (vectorSource != null)
                {
                    if (vectorSource.VectorHeight == 0 && vectorSource.VectorWidth == 0)
                    {
                        if (Element.Height > 0d)
                        {
                            vectorSource.UseDipUnits  = true;
                            vectorSource.VectorHeight = (int)Element.Height;
                        }
                        else if (Element.Width > 0d)
                        {
                            vectorSource.UseDipUnits = true;
                            vectorSource.VectorWidth = (int)Element.Width;
                        }
                        else
                        {
                            vectorSource.UseDipUnits  = false;
                            vectorSource.VectorHeight = 200;
                        }
                    }

                    imageLoader.WithCustomDataResolver(vectorSource.GetVectorDataResolver());
                }

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

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

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

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

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

                // FadeAnimation
                if (Element.FadeAnimationEnabled.HasValue)
                {
                    imageLoader.FadeAnimation(Element.FadeAnimationEnabled.Value);
                }

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

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

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

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

                var element = Element;

                imageLoader.Finish((work) => {
                    element.OnFinish(new CachedImageEvents.FinishEventArgs(work));
                    ImageLoadingFinished(element);
                });

                imageLoader.Success((imageInformation, loadingResult) =>
                {
                    element.OnSuccess(new CachedImageEvents.SuccessEventArgs(imageInformation, loadingResult));
                    _lastImageSource = ffSource;
                });

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

                imageLoader.DownloadStarted((downloadInformation) =>
                                            element.OnDownloadStarted(new CachedImageEvents.DownloadStartedEventArgs(downloadInformation)));

                _currentTask = imageLoader.Into(Control);
            }
        }
コード例 #8
0
        protected virtual Task <IImageLoaderTask> LoadImageAsync(IImageSourceBinding binding, Xamarin.Forms.ImageSource imageSource, TNativeView imageView, CancellationToken cancellationToken)
        {
            TaskParameter parameters = default;

            if (binding.ImageSource == ImageSource.Url)
            {
                var urlSource = (Xamarin.Forms.UriImageSource)((imageSource as IVectorImageSource)?.ImageSource ?? imageSource);
                parameters = ImageService.Instance.LoadUrl(binding.Path, urlSource.CacheValidity);

                if (!urlSource.CachingEnabled)
                {
                    parameters.WithCache(Cache.CacheType.None);
                }
            }
            else if (binding.ImageSource == ImageSource.CompiledResource)
            {
                parameters = ImageService.Instance.LoadCompiledResource(binding.Path);
            }
            else if (binding.ImageSource == ImageSource.ApplicationBundle)
            {
                parameters = ImageService.Instance.LoadFileFromApplicationBundle(binding.Path);
            }
            else if (binding.ImageSource == ImageSource.Filepath)
            {
                parameters = ImageService.Instance.LoadFile(binding.Path);
            }
            else if (binding.ImageSource == ImageSource.Stream)
            {
                parameters = ImageService.Instance.LoadStream(binding.Stream);
            }
            else if (binding.ImageSource == ImageSource.EmbeddedResource)
            {
                parameters = ImageService.Instance.LoadEmbeddedResource(binding.Path);
            }

            if (parameters != null)
            {
                // Enable vector image source
                if (imageSource is IVectorImageSource vect)
                {
                    parameters.WithCustomDataResolver(vect.GetVectorDataResolver());
                }

                var tcs = new TaskCompletionSource <IImageLoaderTask>();

                parameters
                .FadeAnimation(false, false)
                .Error(ex => {
                    tcs.TrySetException(ex);
                })
                .Finish(scheduledWork => {
                    tcs.TrySetResult(scheduledWork as IImageLoaderTask);
                });

                if (cancellationToken.IsCancellationRequested)
                {
                    return(Task.FromResult <IImageLoaderTask>(null));
                }

                var task = GetImageLoaderTask(parameters, imageView);

                if (cancellationToken != null)
                {
                    cancellationToken.Register(() =>
                    {
                        try
                        {
                            task?.Cancel();
                        }
                        catch { }
                    });
                }

                return(tcs.Task);
            }

            return(Task.FromResult <IImageLoaderTask>(null));
        }