public SvgImageSource(Xamarin.Forms.ImageSource imageSource, int vectorWidth, int vectorHeight, bool useDipUnits, Dictionary <string, string> replaceStringMap = null)
 {
     ImageSource      = imageSource;
     VectorWidth      = vectorWidth;
     VectorHeight     = vectorHeight;
     UseDipUnits      = useDipUnits;
     ReplaceStringMap = replaceStringMap;
 }
Beispiel #2
0
 public imagelist(Xamarin.Forms.ImageSource Image, string Text, string Number, string Email, Xamarin.Forms.ImageSource Background, int Index)
 {
     this.image      = Image;
     this.text       = Text;
     this.number     = Number;
     this.index      = Index;
     this.email      = Email;
     this.background = Background;
 }
Beispiel #3
0
        /// <summary>
        /// 根据字符串获取颜色
        /// </summary>
        /// <param name="args"></param>
        /// <returns></returns>
        public static Xamarin.Forms.ImageSource String2ImageSource(string args)
        {
            Xamarin.Forms.ImageSource result = null;

            try
            {
                if (args.StartsWith(ImageSourceUtils.StreamType, StringComparison.CurrentCultureIgnoreCase) == true)
                {
                    result = Xamarin.Forms.ImageSource.FromStream(() =>
                    {
                        string base64Str = args.Substring(ImageSourceUtils.StreamType.Length, args.Length - ImageSourceUtils.StreamType.Length);
                        return(new System.IO.MemoryStream(Convert.FromBase64String(base64Str)));

                        //    //// 遇过的坑
                        //    //// 由于 Write 后, Stream 的 Position 位于 Stream的最后
                        //    //// 导致 ImageSource 获取后无法读取,
                        //    //// 解决方法 Write 后, 重新定位到 Stream 的首位即可
                        //    //System.IO.MemoryStream ms = new System.IO.MemoryStream();
                        //    //byte[] buf = Convert.FromBase64String(base64);
                        //    //ms.Write(buf, 0, buf.Length);
                        //    //ms.Seek(0, System.IO.SeekOrigin.Begin);
                        //    //return ms;
                    });
                }
                else if (args.StartsWith(ImageSourceUtils.UriType, StringComparison.CurrentCultureIgnoreCase) == true)
                {
                    string uriStr = args.Substring(ImageSourceUtils.UriType.Length, args.Length - ImageSourceUtils.UriType.Length);
                    result = Xamarin.Forms.ImageSource.FromUri(new Uri(uriStr));
                }
                else if (args.StartsWith(ImageSourceUtils.ResourceType, StringComparison.CurrentCultureIgnoreCase) == true)
                {
                    string res = args.Substring(ImageSourceUtils.ResourceType.Length, args.Length - ImageSourceUtils.ResourceType.Length);
                    result = Xamarin.Forms.ImageSource.FromResource(res);
                }
                else if (args.StartsWith(ImageSourceUtils.FileType, StringComparison.CurrentCultureIgnoreCase) == true)
                {
                    string filePath = args.Substring(ImageSourceUtils.FileType.Length, args.Length - ImageSourceUtils.FileType.Length);
                    result = Xamarin.Forms.ImageSource.FromFile(filePath);
                }
                else
                {
                    throw new Exception("没有匹配的图片资源类型");
                }

                return(result);
            }
            catch (Exception ex)
            {
                System.Diagnostics.Debug.WriteLine(ex.GetFullInfo());
#if DEBUG
                System.Diagnostics.Debugger.Break();
#endif
                throw ex;
            }
        }
Beispiel #4
0
        public string GetKey(Xamarin.Forms.ImageSource imageSource, object bindingContext)
        {
            var keySource = imageSource as CustomStreamImageSource;

            if (keySource == null)
            {
                return(null);
            }

            return(keySource.Key);
        }
Beispiel #5
0
 public void UpdateDisplayImage(Xamarin.Forms.ImageSource image)
 {
     try
     {
         DisplayImage = image;
         if (PropertyChanged != null)
         {
             PropertyChanged(this, new PropertyChangedEventArgs("DisplayImage"));
         }
     }
     catch (Exception e)
     {
     }
 }
Beispiel #6
0
        /// <summary>
        /// Returns a <see cref="Xamarin.Forms.Image" /> from the <see cref="ImageSource" /> provided.
        /// </summary>
        /// <param name="source">The <see cref="ImageSource" /> to load the image from.</param>
        /// <param name="currentImage">The current image.</param>
        /// <returns>A properly sized image.</returns>
        private static async Task <Image> GetImageAsync(Xamarin.Forms.ImageSource source)
        {
            var image   = new Image();
            var handler = GetHandler(source);

            var imageSource = await handler.LoadImageAsync(source);

            image.Source              = imageSource;
            image.Stretch             = Stretch.UniformToFill;
            image.VerticalAlignment   = VerticalAlignment.Center;
            image.HorizontalAlignment = HorizontalAlignment.Center;

            return(image);
        }
Beispiel #7
0
 internal static string ImageSourceKey(this Xamarin.Forms.ImageSource imageSource)
 {
     if (imageSource.GetValue(Forms9Patch.ImageSource.EmbeddedResourceIdProperty) is string resourceId)
     {
         return("eri:" + resourceId);
     }
     if (imageSource is Xamarin.Forms.FileImageSource fileSource)
     {
         return("file:" + fileSource.File);
     }
     if (imageSource is Xamarin.Forms.UriImageSource uriSource)
     {
         return("uri:" + uriSource.Uri);
     }
     return(null);
 }
Beispiel #8
0
 /// <summary>
 /// Determins if two ImageSources are the same
 /// </summary>
 /// <param name="thisSource"></param>
 /// <param name="otherSource"></param>
 /// <returns></returns>
 public static bool SameAs(this Xamarin.Forms.ImageSource thisSource, Xamarin.Forms.ImageSource otherSource)
 {
     if (thisSource == otherSource)
     {
         return(true);
     }
     if (thisSource is Xamarin.Forms.StreamImageSource thisStreamSource && otherSource is Xamarin.Forms.StreamImageSource otherStreamSource)
     {
         if (thisStreamSource.GetValue(ImageSource.AssemblyProperty) != otherStreamSource.GetValue(ImageSource.AssemblyProperty))
         {
             return(false);
         }
         return(thisStreamSource.GetValue(ImageSource.EmbeddedResourceIdProperty) != otherStreamSource.GetValue(ImageSource.EmbeddedResourceIdProperty) ? false : true);
     }
     return(thisSource is Xamarin.Forms.FileImageSource thisFileSource && otherSource is Xamarin.Forms.FileImageSource otherFileSource && thisFileSource.File == otherFileSource.File);
 }
Beispiel #9
0
 public void UpdateDisplayImage(Image image)
 {
     try
     {
         var regex      = new Regex("data:image.*base64,");
         var base64     = regex.Replace(image.Base64, String.Empty);
         var formsImage = Xamarin.Forms.ImageSource.FromStream(
             () => { return(new MemoryStream(Convert.FromBase64String(base64))); });
         DisplayImage = formsImage;
         if (PropertyChanged != null)
         {
             PropertyChanged(this, new PropertyChangedEventArgs("DisplayImage"));
         }
     } catch (Exception e)
     {
     }
 }
 public void CacheFromFile(String file)
 {
     if (!_store.ContainsKey(file))
     {
         Xamarin.Forms.ImageSource source = Xamarin.Forms.ImageSource.FromFile(file);
         var imageHandler = source.GetLoaderHandler();
         if (imageHandler != null)
         {
             var nativeImage = imageHandler.LoadImageAsync(source, Android.App.Application.Context);
             if (nativeImage != null && nativeImage.Status != TaskStatus.Faulted)
             {
                 _store[file] = nativeImage.Result;
                 System.Diagnostics.Debug.WriteLine("PIC CACHED " + file);
             }
         }
     }
 }
        public async void CacheFromFileAync(String file)
        {
            if (!_store.ContainsKey(file))
            {
                Xamarin.Forms.ImageSource source = Xamarin.Forms.ImageSource.FromFile(file);
                var imageHandler = source.GetLoaderHandler();
                if (imageHandler != null)
                {
                    var nativeImage = await imageHandler.LoadImageAsync(source, null);

                    if (nativeImage != null)
                    {
                        _store[file] = nativeImage;
                    }
                }
            }
        }
        private IImageSourceHandler GetHandler(Xamarin.Forms.ImageSource imageSource)
        {
            IImageSourceHandler returnValue = null;

            if (imageSource is Xamarin.Forms.UriImageSource)
            {
                returnValue = new ImageLoaderSourceHandler();
            }
            else if (imageSource is Xamarin.Forms.FileImageSource)
            {
                returnValue = new FileImageSourceHandler();
            }
            else if (imageSource is Xamarin.Forms.StreamImageSource)
            {
                returnValue = new StreamImagesourceHandler();
            }
            return(returnValue);
        }
Beispiel #13
0
        private static IImageSourceHandler GetHandler(Xamarin.Forms.ImageSource source)
        {
            IImageSourceHandler returnValue = null;

            if (source is Xamarin.Forms.UriImageSource)
            {
                returnValue = new UriImageSourceHandler();
            }
            else if (source is Xamarin.Forms.FileImageSource)
            {
                returnValue = new FileImageSourceHandler();
            }
            else if (source is Xamarin.Forms.StreamImageSource)
            {
                returnValue = new StreamImageSourceHandler();
            }
            return(returnValue);
        }
Beispiel #14
0
        public async Task <PImage> LoadImageAsync(Xamarin.Forms.ImageSource imageSource, CancellationToken cancellationToken = default, float scale = 1)
        {
            try
            {
                var source = ImageSourceBinding.GetImageSourceBinding(imageSource, null);
                if (source == null)
                {
                    return(null);
                }

                var result = await LoadImageAsync(source, imageSource, null, cancellationToken).ConfigureAwait(false);

                var target = result?.Target as PImageTarget;
                return(target?.PImage);
            }
            catch (Exception)
            {
                return(null);
            }
        }
Beispiel #15
0
        public async Task <Windows.UI.Xaml.Media.ImageSource> LoadImageAsync(Xamarin.Forms.ImageSource imagesource, CancellationToken cancellationToken = default)
        {
            if (imagesource is GravatarImageSource gis)
            {
                var imageBytes = await gis.GetGravatarAsync();

                if (imageBytes.Length > 0)
                {
                    using (var stream = new MemoryStream(imageBytes))
                    {
                        var bitmapimage = new BitmapImage();
                        await bitmapimage.SetSourceAsync(stream.AsRandomAccessStream());

                        return(bitmapimage);
                    }
                }
            }

            return(null);
        }
Beispiel #16
0
        public async Task <WindowsImageSource> LoadImageAsync(FormsImageSource imagesource, CancellationToken cancellationToken = default)
        {
            var fileInfo = await LoadInternal(imagesource, 1, ApplicationData.Current.LocalCacheFolder.Path);

            BitmapImage bitmap = null;

            try
            {
                await semaphore.WaitAsync();

                if (fileInfo?.Exists ?? false)
                {
                    bitmap = new BitmapImage(new Uri(fileInfo.FullName));
                }
            }
            finally
            {
                semaphore.Release();
            }

            return(bitmap);
        }
        public Task LoadImageAsync(Xamarin.Forms.ImageSource imageSource, ImageView imageView, CancellationToken cancellationToken = default)
        {
            try
            {
                if (!IsValid(imageView))
                {
                    return(Task.CompletedTask);
                }

                var source = ImageSourceBinding.GetImageSourceBinding(imageSource, null);
                if (source == null)
                {
                    imageView.SetImageResource(Android.Resource.Color.Transparent);
                    return(Task.CompletedTask);
                }

                return(LoadImageAsync(source, imageSource, imageView, cancellationToken));
            }
            catch (Exception)
            {
                return(Task.CompletedTask);
            }
        }
        /// <summary>
        /// Creates a model for collection based on existing references
        /// </summary>
        /// <returns>The model.</returns>
        /// <param name="name1">Name1.</param>
        /// <param name="res1">Res1.</param>
        /// <param name="name2">Name2.</param>
        /// <param name="res2">Res2.</param>
        /// <param name="name3">Name3.</param>
        /// <param name="res3">Res3.</param>
        /// <param name="recommendedWidth">Recommended width.</param>
        private Models.DisplayImageRowModel CreateModel(string name1, Xamarin.Forms.ImageSource res1, int rot1,
                                                        string name2, Xamarin.Forms.ImageSource res2, int rot2,
                                                        string name3, Xamarin.Forms.ImageSource res3, int rot3,
                                                        double recommendedWidth)
        {
            return(new Models.DisplayImageRowModel()
            {
                Name1 = name1,
                Image1 = res1,
                Rotation1 = rot1,

                Name2 = name2,
                Image2 = res2,
                Rotation2 = rot2,

                Name3 = name3,
                Image3 = res3,
                Rotation3 = rot3,

                WidthRequest = recommendedWidth,

                TappedCommand = new Xamarin.Forms.Command(TappedCommand)
            });
        }
 public async Task Display(Xamarin.Forms.ImageSource imageSource)
 {
     await Display(null, imageSource);
 }
        private async void UpdateSource()
        {
            Element.SetIsLoading(true);

            Xamarin.Forms.ImageSource source = Element.Source;

            Cancel();
            TaskParameter imageLoader = null;

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

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

                ImageLoadingFinished(Element);
            }
            else if (ffSource.ImageSource == FFImageLoading.Work.ImageSource.Url)
            {
                imageLoader = ImageService.Instance.LoadUrl(ffSource.Path, Element.CacheDuration);
            }
            else if (ffSource.ImageSource == FFImageLoading.Work.ImageSource.CompiledResource)
            {
                imageLoader = ImageService.Instance.LoadCompiledResource(ffSource.Path);
            }
            else if (ffSource.ImageSource == FFImageLoading.Work.ImageSource.ApplicationBundle)
            {
                imageLoader = ImageService.Instance.LoadFileFromApplicationBundle(ffSource.Path);
            }
            else if (ffSource.ImageSource == FFImageLoading.Work.ImageSource.Filepath)
            {
                imageLoader = ImageService.Instance.LoadFile(ffSource.Path);
            }
            else if (ffSource.ImageSource == FFImageLoading.Work.ImageSource.Stream)
            {
                imageLoader = ImageService.Instance.LoadStream(ffSource.Stream);
            }
            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)
                {
                    var placeholderSource = await ImageSourceBinding.GetImageSourceBinding(Element.LoadingPlaceholder, Element).ConfigureAwait(false);

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

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

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

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

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

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

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

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

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

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

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

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

                var element = Element;

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

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

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

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

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

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

                element.SetupOnBeforeImageLoading(imageLoader);

                _currentTask = imageLoader.Into(Control);
            }
        }
Beispiel #21
0
        /// <summary>
        /// Converts CapsInsets (non-stretchable insets of image) to Forms9Patch.RangeLists
        /// </summary>
        /// <param name="capInsets"></param>
        /// <param name="bitmapWidth"></param>
        /// <param name="bitmapHeight"></param>
        /// <param name="imageSource"></param>
        /// <param name="ninePatchSource"></param>
        /// <returns></returns>
        public static RangeLists ToRangeLists(this Xamarin.Forms.Thickness capInsets, int bitmapWidth, int bitmapHeight, Xamarin.Forms.ImageSource imageSource, bool ninePatchSource)
        {
            if (capInsets.Left > 0 || capInsets.Right > 0 || capInsets.Top > 0 || capInsets.Bottom > 0)
            {
                int  offset     = 0;
                bool normalized = (capInsets.Left < 1 && capInsets.Right < 1);
                var  scale      = (float)imageSource.GetValue(ImageSource.ImageScaleProperty);
                var  capsX      = new List <Range>();
                if (capInsets.Left > 0 || capInsets.Right > 0)
                {
                    var start = (float)System.Math.Round(capInsets.Left * (normalized ? bitmapWidth : scale) + offset);
                    var end   = (float)System.Math.Round(bitmapWidth - 1 - capInsets.Right * (normalized ? bitmapWidth : scale) + offset);
                    if (start < 0)
                    {
                        start = 0;
                    }
                    if (end > bitmapWidth - 1)
                    {
                        end = bitmapWidth - 1;
                    }
                    if (end < 0)
                    {
                        end = 0;
                    }
                    if (start >= end)
                    {
                        start = end;
                    }
                    if (start > 0)
                    {
                        capsX.Add(new Range(0, start - 1, false));
                    }
                    capsX.Add(new Range(start, end, true));
                    if (end < bitmapWidth - 1)
                    {
                        capsX.Add(new Range(end, bitmapWidth - 1, false));
                    }
                }
                else
                {
                    capsX.Add(new Range(0, bitmapWidth - 1, true));
                }
                normalized = (capInsets.Top < 1 && capInsets.Bottom < 1);
                var capsY = new List <Range>();
                if (capInsets.Top > 0 || capInsets.Bottom > 0)
                {
                    var start = (float)System.Math.Round(capInsets.Top * (normalized ? bitmapHeight : scale) + offset);
                    var end   = (float)System.Math.Round(bitmapHeight - 1 - capInsets.Bottom * (normalized ? bitmapHeight : scale) + offset);
                    if (start < 0)
                    {
                        start = 0;
                    }
                    if (end > bitmapHeight - 1)
                    {
                        end = bitmapHeight - 1;
                    }
                    if (end < 0)
                    {
                        end = 0;
                    }
                    if (start >= end)
                    {
                        start = end;
                    }
                    if (start > 0)
                    {
                        capsY.Add(new Range(0, start - 1, false));
                    }
                    capsY.Add(new Range(start, end, true));
                    if (end < bitmapHeight - 1)
                    {
                        capsY.Add(new Range(end, bitmapHeight - 1, false));
                    }
                }
                else
                {
                    capsY.Add(new Range(0, bitmapHeight - 1, true));
                }
                return(new RangeLists
                {
                    PatchesX = capsX,
                    PatchesY = capsY
                });
            }
            return(null);

            /*
             * if (capInsets.Left > 0 || capInsets.Right > 0 || capInsets.Top > 0 || capInsets.Bottom > 0)
             * {
             *  int offset = 0;// ninePatchSource ? 1 : 0;
             *  bool normalized = (capInsets.Left < 1 && capInsets.Right < 1);
             *  var scale = (float)imageSource.GetValue(ImageSource.ImageScaleProperty);
             *  var capsX = new List<Range>();
             *  if (capInsets.Left > 0 || capInsets.Right > 0)
             *  {
             *      var start = capInsets.Left * (normalized ? bitmapWidth : scale) + offset;
             *      //System.Diagnostics.Debug.WriteLine("START: [" + start + "]");
             *      var end = bitmapWidth - 1 - capInsets.Right * (normalized ? bitmapWidth : scale) + offset;
             *      //System.Diagnostics.Debug.WriteLine("END:   [" + end + "]");
             *      if (start < 0)
             *          start = 0;
             *      if (end > bitmapWidth - 1)// - System.Math.Ceiling(Display.Scale))
             *          end = bitmapWidth - 1; // - System.Math.Ceiling(Display.Scale);
             *      if (start >= end)
             *          start = end - 1;
             *      capsX.Add(new Range
             *      {
             *          Start = start,
             *          End = end
             *      });
             *  }
             *  else
             *  {
             *      capsX.Add(new Range
             *      {
             *          Start = 0,
             *          End = bitmapWidth - 1
             *      });
             *  }
             *  normalized = (capInsets.Top < 1 && capInsets.Bottom < 1);
             *  var capsY = new List<Range>();
             *  if (capInsets.Top > 0 || capInsets.Bottom > 0)
             *  {
             *      var start = capInsets.Top * (normalized ? bitmapHeight : scale) + offset;
             *      //System.Diagnostics.Debug.WriteLine("START: [" + start + "]");
             *      var end = bitmapHeight - 1 - capInsets.Bottom * (normalized ? bitmapHeight : scale) + offset;
             *      //System.Diagnostics.Debug.WriteLine("END:   [" + end + "]");
             *      if (start < 0)
             *          start = 0;
             *      if (end > bitmapHeight - 1) // - System.Math.Ceiling(Display.Scale))
             *          end = bitmapHeight - 1;// - System.Math.Ceiling(Display.Scale);
             *      if (start >= end)
             *          start = end - 1;
             *      capsY.Add(new Range
             *      {
             *          Start = start,
             *          End = end,
             *      });
             *  }
             *  else
             *  {
             *      capsY.Add(new Range
             *      {
             *          Start = 0,
             *          End = bitmapHeight - 1,
             *      });
             *  }
             *
             *  return new RangeLists
             *  {
             *      PatchesX = capsX,
             *      PatchesY = capsY
             *  };
             * }
             * return null;
             */
        }
Beispiel #22
0
 /// <summary>
 /// SvgImageSource
 /// </summary>
 /// <param name="imageSource"></param>
 /// <param name="vectorWidth"></param>
 /// <param name="vectorHeight"></param>
 /// <param name="useDipUnits"></param>
 /// <param name="replaceStringMap">Replace string map.</param>
 public SvgImageSource(Xamarin.Forms.ImageSource imageSource, int vectorWidth, int vectorHeight, bool useDipUnits, Dictionary <string, string> replaceStringMap = null)
 {
     throw new Exception(DoNotReference);
 }
Beispiel #23
0
 public MapboxImageSource(string id, LatLngQuad coordinates, Xamarin.Forms.ImageSource source)
 {
     Id          = id;
     Coordinates = coordinates;
     Source      = source;
 }
Beispiel #24
0
 public IImage CreateImage(Xamarin.Forms.ImageSource source)
 {
     return(new Image(source));
 }
 /// <summary>
 /// SvgImageSource
 /// </summary>
 /// <param name="imageSource"></param>
 /// <param name="vectorWidth"></param>
 /// <param name="vectorHeight"></param>
 /// <param name="useDipUnits"></param>
 public SvgImageSource(Xamarin.Forms.ImageSource imageSource, int vectorWidth, int vectorHeight, bool useDipUnits)
 {
     throw new Exception(DoNotReference);
 }
Beispiel #26
0
        protected virtual Task <IImageLoaderTask> LoadImageAsync(IImageSourceBinding binding, Xamarin.Forms.ImageSource imageSource, TNativeView imageView, CancellationToken cancellationToken)
        {
            TaskParameter parameters = default;

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

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

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

                var tcs = new TaskCompletionSource <IImageLoaderTask>();

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

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

                var task = GetImageLoaderTask(parameters, imageView);

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

                return(tcs.Task);
            }

            return(Task.FromResult <IImageLoaderTask>(null));
        }
        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);
            }
        }
 public SvgImageSource(Xamarin.Forms.ImageSource imageSource, int vectorWidth, int vectorHeight, bool useDipUnits)
 {
     ImageSource = imageSource;
 }