コード例 #1
0
        public static Task <SKImage?> ToSKImageAsync(this ImageSource imageSource, CancellationToken cancellationToken = default)
        {
            if (imageSource == null)
            {
                throw new ArgumentNullException(nameof(imageSource));
            }

            return(imageSource switch
            {
                // 1. first try SkiaSharp sources
                SKImageImageSource iis => FromSkia(iis.Image),
                SKBitmapImageSource bis => FromSkia(SKImage.FromBitmap(bis.Bitmap)),
                SKPixmapImageSource xis => FromSkia(SKImage.FromPixels(xis.Pixmap)),
                SKPictureImageSource pis => FromSkia(SKImage.FromPicture(pis.Picture, pis.Dimensions)),

                // 2. then try Stream sources
                StreamImageSource stream => FromStream(stream.Stream.Invoke(cancellationToken)),
                UriImageSource uri => FromStream(uri.GetStreamAsync(cancellationToken)),

                // 3. finally, use the handlers
                FileImageSource file => FromHandler(PlatformToSKImageAsync(file, cancellationToken)),
                FontImageSource font => FromHandler(PlatformToSKImageAsync(font, cancellationToken)),

                // 4. all is lost
                _ => throw new ArgumentException("Unable to determine the type of image source.", nameof(imageSource))
            });
コード例 #2
0
		public void SecondCallLoadFromCache ()
		{
			var loader = new UriImageSource { 
				Uri = new Uri ("http://foo.com/Images/crimson.jpg"),
			};
			Assert.AreEqual (0, networkcalls);

			using (var s0 = loader.GetStreamAsync ().Result) {
				Assert.AreEqual (79109, s0.Length);
				Assert.AreEqual (1, networkcalls);
			}
				
			using (var s1 = loader.GetStreamAsync ().Result) {
				Assert.AreEqual (79109, s1.Length);
				Assert.AreEqual (1, networkcalls);
			}
		}
コード例 #3
0
        public void ConcurrentCallsOnSameUriAreQueued()
        {
            var loader = new UriImageSource {
                Uri = new Uri("http://foo.com/Images/crimson.jpg"),
            };

            Assert.AreEqual(0, networkcalls);

            var t0 = loader.GetStreamAsync();
            var t1 = loader.GetStreamAsync();

            //var s0 = t0.Result;
            using (var s1 = t1.Result) {
                Assert.AreEqual(1, networkcalls);
                Assert.AreEqual(79109, s1.Length);
            }
        }
コード例 #4
0
		public void LoadImageFromStream ()
		{
			var loader = new UriImageSource { 
				Uri = new Uri ("http://foo.com/Images/crimson.jpg"),
			};
			Stream s0 = loader.GetStreamAsync ().Result;

			Assert.AreEqual (79109, s0.Length);
		}
コード例 #5
0
        public void DoNotKeepFailedRetrieveInCache()
        {
            var loader = new UriImageSource {
                Uri = new Uri("http://foo.com/missing.png"),
            };

            Assert.AreEqual(0, networkcalls);

            var s0 = loader.GetStreamAsync().Result;

            Assert.IsNull(s0);
            Assert.AreEqual(1, networkcalls);

            var s1 = loader.GetStreamAsync().Result;

            Assert.IsNull(s1);
            Assert.AreEqual(2, networkcalls);
        }
コード例 #6
0
        public void SecondCallLoadFromCache()
        {
            var loader = new UriImageSource {
                Uri = new Uri("http://foo.com/Images/crimson.jpg"),
            };

            Assert.AreEqual(0, networkcalls);

            using (var s0 = loader.GetStreamAsync().Result) {
                Assert.AreEqual(79109, s0.Length);
                Assert.AreEqual(1, networkcalls);
            }

            using (var s1 = loader.GetStreamAsync().Result) {
                Assert.AreEqual(79109, s1.Length);
                Assert.AreEqual(1, networkcalls);
            }
        }
コード例 #7
0
        public void LoadImageFromStream()
        {
            var loader = new UriImageSource {
                Uri = new Uri("http://foo.com/Images/crimson.jpg"),
            };
            Stream s0 = loader.GetStreamAsync().Result;

            Assert.AreEqual(79109, s0.Length);
        }
コード例 #8
0
        public static async Task <IFormsAnimationDrawable> LoadImageAnimationAsync(UriImageSource imagesource, Context context, CancellationToken cancelationToken = default(CancellationToken))
        {
            Uri uri = imagesource?.Uri;
            FormsAnimationDrawable animation = null;

            if (uri != null)
            {
                var options = new BitmapFactory.Options
                {
                    InJustDecodeBounds = true
                };

                using (Stream stream = await imagesource.GetStreamAsync(cancelationToken).ConfigureAwait(false))
                {
                    using (var decoder = new AndroidGIFImageParser(context, options.InDensity, options.InTargetDensity))
                    {
                        try
                        {
                            if (!FileImageSourceHandler.DecodeSynchronously)
                            {
                                await decoder.ParseAsync(stream).ConfigureAwait(false);
                            }
                            else
                            {
                                decoder.ParseAsync(stream).Wait();
                            }

                            animation = decoder.Animation;
                        }
                        catch (GIFDecoderFormatException ex)
                        {
                            System.Diagnostics.Debug.WriteLine(ex.Message);
                            animation = null;
                        }
                    }
                }

                if (animation == null)
                {
                    Log.Warning(nameof(FileImageSourceHandler), "Could not retrieve image or image data was invalid: {0}", imagesource);
                }
            }

            return(animation);
        }
コード例 #9
0
        public async Task <NSImage> LoadImageAsync(
            ImageSource imagesource,
            CancellationToken cancelationToken)
        {
            NSImage        image          = null;
            UriImageSource uriImageSource = imagesource as UriImageSource;

            if (uriImageSource != null && uriImageSource.Uri != null)
            {
                using (Stream stream = await uriImageSource.GetStreamAsync(cancelationToken).ConfigureAwait(false))
                {
                    if (stream != null)
                    {
                        image = new NSImage(NSData.FromStream(stream));
                    }
                }
            }
            return(image);
        }
コード例 #10
0
        static public async Task <FormsCAKeyFrameAnimation> CreateAnimationFromUriImageSourceAsync(UriImageSource imageSource, CancellationToken cancelationToken = default(CancellationToken))
        {
            FormsCAKeyFrameAnimation animation = null;

            if (imageSource?.Uri != null)
            {
                using (var streamImage = await imageSource.GetStreamAsync(cancelationToken).ConfigureAwait(false))
                {
                    if (streamImage != null)
                    {
                        using (var parsedImageSource = CGImageSource.FromData(NSData.FromStream(streamImage)))
                        {
                            animation = ImageAnimationHelper.CreateAnimationFromCGImageSource(parsedImageSource);
                        }
                    }
                }
            }

            return(animation);
        }
コード例 #11
0
		public void DoNotKeepFailedRetrieveInCache ()
		{
			var loader = new UriImageSource { 
				Uri = new Uri ("http://foo.com/missing.png"),
			};
			Assert.AreEqual (0, networkcalls);

			var s0 = loader.GetStreamAsync ().Result;
			Assert.IsNull (s0);
			Assert.AreEqual (1, networkcalls);

			var s1 = loader.GetStreamAsync ().Result;
			Assert.IsNull (s1);
			Assert.AreEqual (2, networkcalls);
		}
コード例 #12
0
		public void ConcurrentCallsOnSameUriAreQueued ()
		{
			var loader = new UriImageSource { 
				Uri = new Uri ("http://foo.com/Images/crimson.jpg"),
			};
			Assert.AreEqual (0, networkcalls);

			var t0 = loader.GetStreamAsync ();
			var t1 = loader.GetStreamAsync ();

			//var s0 = t0.Result;
			using (var s1 = t1.Result) {
				Assert.AreEqual (1, networkcalls);
				Assert.AreEqual (79109, s1.Length);
			}
		}