void UpdateBitmap(CachedImageView imageView, CachedImage image, CachedImage previousImage)
        {
            CancelIfNeeded();

            if (image == null || imageView == null || imageView.Handle == IntPtr.Zero || _isDisposed)
            {
                return;
            }

            var ffSource = ImageSourceBinding.GetImageSourceBinding(image.Source, image);

            if (ffSource == null)
            {
                if (_lastImageSource == null)
                {
                    return;
                }

                _lastImageSource = null;
                imageView.SetImageResource(global::Android.Resource.Color.Transparent);
                return;
            }

            if (previousImage != null && !ffSource.Equals(_lastImageSource))
            {
                _lastImageSource = null;
                imageView.SkipInvalidate();
                Control.SetImageResource(global::Android.Resource.Color.Transparent);
            }

            image.SetIsLoading(true);

            var           placeholderSource      = ImageSourceBinding.GetImageSourceBinding(image.LoadingPlaceholder, image);
            var           errorPlaceholderSource = ImageSourceBinding.GetImageSourceBinding(image.ErrorPlaceholder, image);
            TaskParameter imageLoader;

            image.SetupOnBeforeImageLoading(out imageLoader, ffSource, placeholderSource, errorPlaceholderSource);

            if (imageLoader != null)
            {
                var finishAction = imageLoader.OnFinish;
                var sucessAction = imageLoader.OnSuccess;

                imageLoader.Finish((work) =>
                {
                    finishAction?.Invoke(work);
                    ImageLoadingFinished(image);
                });

                imageLoader.Success((imageInformation, loadingResult) =>
                {
                    sucessAction?.Invoke(imageInformation, loadingResult);
                    _lastImageSource = ffSource;
                });

                _currentTask = imageLoader.Into(imageView);
            }
        }
        private void UpdateBitmap(CachedImage previous = null)
        {
            if (previous == null || !object.Equals(previous.Source, Element.Source))
            {
                Xamarin.Forms.ImageSource source = Element.Source;
                var imageView = Control;

                if (imageView == null)
                {
                    return;
                }

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

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

                    var ffSource = ImageSourceBinding.GetImageSourceBinding(source);

                    if (ffSource == null)
                    {
                        if (imageView != null)
                        {
                            imageView.SetImageDrawable(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 = ImageSourceBinding.GetImageSourceBinding(Element.LoadingPlaceholder);
                            if (placeholderSource != null)
                            {
                                imageLoader.LoadingPlaceholder(placeholderSource.Path, placeholderSource.ImageSource);
                            }
                        }

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

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

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

                        // TransparencyChannel
                        if (Element.TransparencyEnabled.HasValue)
                        {
                            imageLoader.TransparencyChannel(Element.TransparencyEnabled.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);

                        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)));

                        _currentTask = imageLoader.Into(imageView);
                    }
                }
            }
        }
示例#3
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);
                }
            }
        }