コード例 #1
0
            public async void SetSource(ImageSource source)
            {
                try
                {
                    if (!string.IsNullOrWhiteSpace(_cacheKey))
                    {
                        await ImageService.Instance.InvalidateCacheEntryAsync(_cacheKey, FFImageLoading.Cache.CacheType.Memory, true);
                    }

                    if (!string.IsNullOrWhiteSpace(_refinedCacheKey))
                    {
                        await ImageService.Instance.InvalidateCacheEntryAsync(_cacheKey, FFImageLoading.Cache.CacheType.Memory, true);
                    }

                    _imageGuid       = Guid.NewGuid();
                    _cacheKey        = _imageGuid.ToString();
                    _refinedCacheKey = $"{_imageGuid.ToString()}-Refined";
                    TaskParameter task        = null;
                    TaskParameter taskRefined = null;

                    var fileSource = source as FileImageSource;
                    if (fileSource != null)
                    {
                        task        = ImageService.Instance.LoadFile(fileSource.File);
                        taskRefined = ImageService.Instance.LoadFile(fileSource.File);
                    }

                    var urlSource = source as UriImageSource;
                    if (urlSource != null)
                    {
                        task        = ImageService.Instance.LoadUrl(urlSource.Uri?.OriginalString);
                        taskRefined = ImageService.Instance.LoadUrl(urlSource.Uri?.OriginalString);
                    }

                    var streamSource = source as StreamImageSource;
                    if (streamSource != null)
                    {
                        task        = ImageService.Instance.LoadStream(streamSource.Stream);
                        taskRefined = ImageService.Instance.LoadStream(streamSource.Stream);
                    }

                    using (var stream = await task.DownSample(PreviewResolution, PreviewResolution).AsJPGStreamAsync(90))
                    {
                        byte[] bytes = StreamToByteArray(stream);
                        _source = ImageSource.FromStream(() => new MemoryStream(bytes));
                    }

                    using (var streamRefined = await taskRefined.DownSample(RefinedResolution, RefinedResolution).AsJPGStreamAsync(90))
                    {
                        byte[] bytes = StreamToByteArray(streamRefined);
                        _refinedSource = ImageSource.FromStream(() => new MemoryStream(bytes));
                    }

                    LoadRefinedImage();
                }
                catch (Exception ex)
                {
                    System.Diagnostics.Debug.WriteLine(ex);
                }
            }
コード例 #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);
            }
        }
コード例 #3
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).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).ConfigureAwait(false);

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

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

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

                // 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);
                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);
            }
        }
コード例 #4
0
        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);
                    }
                }
            }
        }
コード例 #5
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);
            }
        }
コード例 #6
0
        private void SetImage(CachedImage oldElement = null)
        {
            Xamarin.Forms.ImageSource source = Element.Source;

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

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

            Element.SetIsLoading(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));
                }

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

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

                _currentTask = imageLoader.Into(Control);
            }
        }
コード例 #7
0
        private void SetImage(CachedImage oldElement = null)
        {
            Xamarin.Forms.ImageSource source = base.Element.Source;
            if (oldElement != null)
            {
                Xamarin.Forms.ImageSource source2 = oldElement.Source;
                if (object.Equals(source2, source))
                {
                    return;
                }
                if (source2 is FileImageSource && source is FileImageSource && ((FileImageSource)source2).File == ((FileImageSource)source).File)
                {
                    return;
                }
            }

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

            TaskParameter imageLoader = null;

            if (source == null)
            {
                Control.Image = null;
                ImageLoadingFinished(Element);
            }
            else if (source is UriImageSource)
            {
                var urlSource = (UriImageSource)source;

                imageLoader = string.IsNullOrWhiteSpace(Element.LoadingPlaceholder) ?
                              ImageService.Instance.LoadUrl(urlSource.Uri.ToString(), Element.CacheDuration) :
                              ImageService.Instance.LoadUrl(urlSource.Uri.ToString(), Element.CacheDuration).LoadingPlaceholder(Element.LoadingPlaceholder);
            }
            else if (source is FileImageSource)
            {
                var fileSource = (FileImageSource)source;
                Control.Image = UIImage.FromBundle(fileSource.File);
                ImageLoadingFinished(Element);
            }
            else
            {
                throw new NotImplementedException("ImageSource type not supported");
            }

            if (imageLoader != null)
            {
                if ((int)Element.DownsampleHeight != 0 || (int)Element.DownsampleWidth != 0)
                {
                    if (Element.DownsampleHeight > Element.DownsampleWidth)
                    {
                        imageLoader.DownSample(height: (int)Element.DownsampleWidth);
                    }
                    else
                    {
                        imageLoader.DownSample(width: (int)Element.DownsampleHeight);
                    }
                }

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

                imageLoader.TransparencyChannel(Element.TransparencyEnabled);

                imageLoader.Finish((work) => ImageLoadingFinished(Element));
                imageLoader.Into(Control);
            }
        }
コード例 #8
0
ファイル: ImageCropView.cs プロジェクト: ConchNCode/munch
            public async void SetSource(ImageSource source)
            {
                try
                {
                    await _lock.WaitAsync();

                    if (!string.IsNullOrWhiteSpace(_cacheKey))
                    {
                        await ImageService.Instance.InvalidateCacheEntryAsync(_cacheKey, FFImageLoading.Cache.CacheType.Memory, true);
                    }

                    if (!string.IsNullOrWhiteSpace(_refinedCacheKey))
                    {
                        await ImageService.Instance.InvalidateCacheEntryAsync(_cacheKey, FFImageLoading.Cache.CacheType.Memory, true);
                    }

                    if (source == null)
                    {
                        _cacheKey        = null;
                        _refinedCacheKey = null;
                        _source          = null;
                        _refinedSource   = null;
                        _originalSource  = null;
                        Source           = null;
                        return;
                    }

                    var imageGuid = Guid.NewGuid();
                    _cacheKey        = imageGuid.ToString();
                    _refinedCacheKey = $"{imageGuid.ToString()}-Refined";
                    _originalSource  = source;
                    TaskParameter task        = null;
                    TaskParameter taskRefined = null;

                    var fileSource = source as FileImageSource;
                    if (fileSource != null)
                    {
                        task        = ImageService.Instance.LoadFile(fileSource.File);
                        taskRefined = ImageService.Instance.LoadFile(fileSource.File);
                        Stream      = null;
                        Path        = fileSource.File;
                        SourceType  = FFImageLoading.Work.ImageSource.Filepath;
                    }

                    var urlSource = source as UriImageSource;
                    if (urlSource != null)
                    {
                        task        = ImageService.Instance.LoadUrl(urlSource.Uri?.OriginalString);
                        taskRefined = ImageService.Instance.LoadUrl(urlSource.Uri?.OriginalString);
                        Stream      = null;
                        Path        = urlSource.Uri?.OriginalString;
                        SourceType  = FFImageLoading.Work.ImageSource.Url;
                    }

                    var streamSource = source as StreamImageSource;
                    if (streamSource != null)
                    {
                        task        = ImageService.Instance.LoadStream(streamSource.Stream);
                        taskRefined = ImageService.Instance.LoadStream(streamSource.Stream);
                        Stream      = streamSource.Stream;
                        Path        = null;
                        SourceType  = FFImageLoading.Work.ImageSource.Stream;
                    }

                    if (ImageRotation != 0)
                    {
                        var rotateTransformation = new RotateTransformation(Math.Abs(ImageRotation), ImageRotation < 0)
                        {
                            Resize = true
                        };

                        task.Transform(rotateTransformation);
                        taskRefined.Transform(rotateTransformation);
                    }

                    using (var stream = await task.DownSample(PreviewResolution, PreviewResolution).AsJPGStreamAsync(90))
                    {
                        byte[] bytes = StreamToByteArray(stream);
                        _source = ImageSource.FromStream(() => new MemoryStream(bytes));
                    }

                    using (var streamRefined = await taskRefined.DownSample(RefinedResolution, RefinedResolution).AsJPGStreamAsync(90))
                    {
                        byte[] bytes = StreamToByteArray(streamRefined);
                        _refinedSource = ImageSource.FromStream(() => new MemoryStream(bytes));
                    }

                    LoadRefinedImage();
                }
                catch (Exception ex)
                {
                    System.Diagnostics.Debug.WriteLine(ex);
                }
                finally
                {
                    _lock.Release();
                }
            }
コード例 #9
0
        private void UpdateBitmap(CachedImage previous = null)
        {
            Xamarin.Forms.ImageSource source = null;
            var vectorSource = Element.Source as IVectorImageSource;

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

            var imageView = Control;

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

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

            ((IElementController)Element).SetValueFromRenderer(CachedImage.IsLoadingPropertyKey, 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);
                }

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

                    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(imageView);
                }
            }
        }
コード例 #10
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="imageview">ImageViewAsync View</param>
        /// <param name="imageUrl">Url of the image</param>
        /// <param name="transform">For CircleTrancform and border white color = 1 / For RoundedTransformation(10) = 2 / For RoundedTransformation with border = 3 / For CircleTrancform and border MainColor = 4 / For RoundedTransformation(30) = 5 </param>
        ///  <param name="downSample">DownSample the Image size by deafault its true</param>
        public static void Load_Image(ImageViewAsync imageview, string imagePlaceholder, string imageUrl,
                                      int transform = 0, bool downSample = true, int transformRadius = 5)
        {
            try
            {
                TaskParameter imageTrancform = ImageService.Instance.LoadCompiledResource(imagePlaceholder);

                if (imageUrl.Contains("d-avatar.jpg"))
                {
                    imageTrancform = ImageService.Instance.LoadCompiledResource("no_profile_image.png");
                }
                else if (imageUrl.Contains("d-cover.jpg"))
                {
                    imageTrancform = ImageService.Instance.LoadCompiledResource("Cover_image.jpg");
                }
                else if (imageUrl.Contains("http"))
                {
                    if (!String.IsNullOrEmpty(imageUrl) &&
                        (imageUrl.Contains("http") || imageUrl.Contains("file://") || imageUrl.Split('/').Count() > 1))
                    {
                        imageTrancform = ImageService.Instance.LoadUrl(imageUrl);
                    }
                    else if (!String.IsNullOrEmpty(imageUrl))
                    {
                        imageTrancform = ImageService.Instance.LoadCompiledResource(imageUrl);
                    }
                    else
                    {
                        imageTrancform = ImageService.Instance.LoadCompiledResource(imagePlaceholder);
                    }
                }
                else
                {
                    var file = Android.Net.Uri.FromFile(new Java.IO.File(imageUrl));
                    imageTrancform = ImageService.Instance.LoadFile(file.Path);
                }

                imageTrancform.TransformPlaceholders(true).Retry(3, 5000).Error(OnError);

                if (downSample)
                {
                    imageTrancform.DownSampleMode(InterpolationMode.Default);
                }

                if (transform == 1)
                {
                    imageTrancform.Transform(new CircleTransformation(transformRadius, "#ffffff"));
                }

                if (transform == 2)
                {
                    imageTrancform.Transform(new RoundedTransformation(10));
                }

                if (transform == 3)
                {
                    imageTrancform.Transform(new RoundedTransformation(10, 2, 2, 10, "#ffffff"));
                }

                if (transform == 4)
                {
                    imageTrancform.Transform(new CircleTransformation(5, AppSettings.MainColor));
                }

                if (transform == 5)
                {
                    imageTrancform.Transform(new RoundedTransformation(30));
                }

                imageTrancform.LoadingPlaceholder(imagePlaceholder, ImageSource.CompiledResource);
                imageTrancform.ErrorPlaceholder(imagePlaceholder, ImageSource.CompiledResource);

                imageTrancform.Success((information, result) =>
                {
                    if (information.OriginalHeight > information.OriginalWidth)
                    {
                        if (information.OriginalHeight > 200 && information.OriginalHeight < 400)
                        {
                            imageTrancform.DownSample(height: 320);
                            imageview.SetMinimumHeight(150);
                        }
                        else if (information.OriginalHeight > 400 && information.OriginalHeight < 500)
                        {
                            imageTrancform.DownSample(height: 320);
                            imageview.SetMinimumHeight(160);
                        }
                        else if (information.OriginalHeight > 500 && information.OriginalHeight < 1000)
                        {
                            imageTrancform.DownSample(height: 420);
                            imageview.SetMinimumHeight(170);
                        }

                        if (information.OriginalHeight > 1000 && information.OriginalHeight < 2000)
                        {
                            imageTrancform.DownSample(width: 200);
                            imageview.SetMinimumHeight(180);
                        }
                    }
                    else
                    {
                        if (information.OriginalHeight > 200 && information.OriginalWidth < 500)
                        {
                            imageTrancform.DownSample(width: 210);
                            imageTrancform.DownSample(height: 210);
                            imageview.SetMinimumHeight(110);
                        }
                        else if (information.OriginalHeight < 500 && information.OriginalWidth < 1000)
                        {
                            imageTrancform.DownSample(width: 260);
                            imageTrancform.DownSample(height: 260);
                            imageview.SetMinimumHeight(130);
                            imageview.SetMinimumWidth(180);
                        }
                        else if (information.OriginalHeight < 1000 && information.OriginalWidth < 2000)
                        {
                            imageTrancform.DownSample(width: information.OriginalWidth / 2);
                            imageTrancform.DownSample(height: information.OriginalHeight / 2);
                            imageview.SetMinimumHeight(140);
                            imageview.SetMinimumWidth(210);
                        }
                        else if (information.OriginalHeight < 2000 && information.OriginalWidth < 2500)
                        {
                            imageTrancform.DownSample(width: information.OriginalWidth / 3);
                            imageTrancform.DownSample(height: information.OriginalHeight / 3);
                            imageview.SetMinimumHeight(150);
                            imageview.SetMinimumWidth(230);
                        }
                    }

                    if (transform == 2)
                    {
                        imageTrancform.Transform(new RoundedTransformation(10));
                    }
                });

                imageTrancform.Into(imageview);
            }
            catch (Exception exception)
            {
                Console.WriteLine(exception);
            }
        }
コード例 #11
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)
            {
                // 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);
            }
        }
コード例 #12
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="imageview">ImageViewAsync View</param>
        /// <param name="imageUrl">Url of the image</param>
        /// <param name="transform">For CircleTrancform and border white color = 1 / For RoundedTransformation(10) = 2 / For RoundedTransformation with border = 3 / For CircleTrancform and border MainColor = 4 / For RoundedTransformation(30) = 5 </param>
        ///  <param name="downSample">DownSample the Image size by deafault its true</param>
        public static void Load_Image(ImageViewAsync imageview, string imagePlaceholder, string imageUrl, int transform = 0, bool downSample = true, int transformRadius = 5)
        {
            try
            {
                TaskParameter imageTrancform = ImageService.Instance.LoadCompiledResource(imagePlaceholder);

                if (imageUrl.Contains("d-avatar.jpg"))
                {
                    imageTrancform = ImageService.Instance.LoadCompiledResource("no_profile_image.png");
                }
                else if (imageUrl.Contains("d-cover.jpg"))
                {
                    imageTrancform = ImageService.Instance.LoadCompiledResource("Cover_image.jpg");
                }
                else if (imageUrl.Contains("http"))
                {
                    if (!String.IsNullOrEmpty(imageUrl) && (imageUrl.Contains("http") || imageUrl.Contains("file://") || imageUrl.Split('/').Count() > 1))
                    {
                        imageTrancform = ImageService.Instance.LoadUrl(imageUrl);
                    }
                    else if (!String.IsNullOrEmpty(imageUrl))
                    {
                        imageTrancform = ImageService.Instance.LoadCompiledResource(imageUrl);
                    }
                    else
                    {
                        imageTrancform = ImageService.Instance.LoadCompiledResource(imagePlaceholder);
                    }
                }
                else
                {
                    var file = Android.Net.Uri.FromFile(new Java.IO.File(imageUrl));
                    imageTrancform = ImageService.Instance.LoadFile(file.Path);
                }

                imageTrancform.TransformPlaceholders(true).Retry(3, 5000).Error(OnError);
                imageTrancform.DownSampleMode(InterpolationMode.Default);
                imageTrancform.BitmapOptimizations(true);

                if (downSample)
                {
                    imageTrancform.Success((information, result) =>
                    {
                        if (information.OriginalHeight > 1000 || information.OriginalWidth > 1000)
                        {
                            imageTrancform.DownSample(250, 250);
                            // imageTrancform.DownSampleInDip(200, 200, false);
                        }
                        else if (information.OriginalHeight > 300 && information.OriginalWidth > 300)
                        {
                            imageTrancform.DownSampleInDip(150, 150, false);
                            // imageTrancform.DownSample(200, 200);
                        }
                        else if (information.OriginalHeight > 200 && information.OriginalWidth > 200)
                        {
                            imageTrancform.DownSample(100, 100);
                        }
                        else if (information.OriginalHeight > 100 && information.OriginalWidth > 100)
                        {
                            imageTrancform.DownSample(40, 40);
                        }
                        else
                        {
                            imageTrancform.DownSample(25, 25);
                        }

                        if (transform == 2)
                        {
                            imageTrancform.Transform(new RoundedTransformation(10));
                        }
                    });
                }

                if (transform == 1)
                {
                    imageTrancform.Transform(new CircleTransformation(transformRadius, "#ffffff"));
                }

                if (transform == 2)
                {
                    imageTrancform.Transform(new RoundedTransformation(10));
                }

                if (transform == 3)
                {
                    imageTrancform.Transform(new RoundedTransformation(10, 2, 2, 10, "#ffffff"));
                }

                if (transform == 4)
                {
                    imageTrancform.Transform(new CircleTransformation(5, Settings.MainColor));
                }

                if (transform == 5)
                {
                    imageTrancform.Transform(new RoundedTransformation(30));
                }

                imageTrancform.LoadingPlaceholder(imagePlaceholder, ImageSource.CompiledResource);
                imageTrancform.ErrorPlaceholder(imagePlaceholder, ImageSource.CompiledResource);

                imageTrancform.Into(imageview);
            }
            catch (Exception exception)
            {
                Crashes.TrackError(exception);
            }
        }
コード例 #13
0
        private void SetImage(CachedImage oldElement = null)
        {
            Xamarin.Forms.ImageSource source = Element.Source;

            if (oldElement != null)
            {
                Xamarin.Forms.ImageSource source2 = oldElement.Source;
                if (object.Equals(source2, source))
                {
                    return;
                }
                if (source2 is FileImageSource && source is FileImageSource && ((FileImageSource)source2).File == ((FileImageSource)source).File)
                {
                    return;
                }
            }

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

            Cancel(this, EventArgs.Empty);
            TaskParameter imageLoader = null;

            var ffSource = ImageSourceBinding.GetImageSourceBinding(source);

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

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

            if (imageLoader != null)
            {
                // 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.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);
                }

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

                var element = Element;

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

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

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

                _currentTask = imageLoader.Into(Control);
            }
        }