Ejemplo n.º 1
0
        static public FormsCAKeyFrameAnimation CreateAnimationFromCGImageSource(CGImageSource imageSource)
        {
            FormsCAKeyFrameAnimation animation = null;
            float repeatCount = float.MaxValue;
            var   imageCount  = imageSource.ImageCount;

            if (imageCount <= 0)
            {
                return(null);
            }

            using (var imageData = new ImageDataHelper(imageCount))
            {
                if (imageSource.TypeIdentifier == "com.compuserve.gif")
                {
                    var imageProperties = imageSource.GetProperties(null);
                    using (var gifImageProperties = imageProperties?.Dictionary[ImageIO.CGImageProperties.GIFDictionary])
                        using (var repeatCountValue = gifImageProperties?.ValueForKey(ImageIO.CGImageProperties.GIFLoopCount))
                        {
                            if (repeatCountValue != null)
                            {
                                float.TryParse(repeatCountValue.ToString(), out repeatCount);
                            }
                            else
                            {
                                repeatCount = 1;
                            }

                            if (repeatCount == 0)
                            {
                                repeatCount = float.MaxValue;
                            }
                        }
                }

                for (int i = 0; i < imageCount; i++)
                {
                    imageData.AddFrameData(i, imageSource);
                }

                animation = imageData.CreateKeyFrameAnimation();
                if (animation != null)
                {
                    animation.RemovedOnCompletion = false;
                    animation.KeyPath             = "contents";
                    animation.RepeatCount         = repeatCount;
                    animation.Width  = imageData.Width;
                    animation.Height = imageData.Height;

                    if (imageCount == 1)
                    {
                        animation.Duration = double.MaxValue;
                        animation.KeyTimes = null;
                    }
                }
            }

            return(animation);
        }
Ejemplo n.º 2
0
        public async Task <FormsCAKeyFrameAnimation> LoadImageAnimationAsync(ImageSource imagesource, CancellationToken cancelationToken = default(CancellationToken), float scale = 1)
        {
            FormsCAKeyFrameAnimation animation = await ImageAnimationHelper.CreateAnimationFromUriImageSourceAsync(imagesource as UriImageSource, cancelationToken).ConfigureAwait(false);

            if (animation == null)
            {
                Controls.Internals.Log.Warning(nameof(FileImageSourceHandler), "Could not find image: {0}", imagesource);
            }

            return(animation);
        }
Ejemplo n.º 3
0
        public Task <FormsCAKeyFrameAnimation> LoadImageAnimationAsync(ImageSource imagesource, CancellationToken cancelationToken = default(CancellationToken), float scale = 1)
        {
            FormsCAKeyFrameAnimation animation = ImageAnimationHelper.CreateAnimationFromFileImageSource(imagesource as FileImageSource);

            if (animation == null)
            {
                Controls.Internals.Log.Warning(nameof(FileImageSourceHandler), "Could not find image: {0}", imagesource);
            }

            return(Task.FromResult(animation));
        }
Ejemplo n.º 4
0
        public async Task <FormsCAKeyFrameAnimation> LoadImageAnimationAsync(ImageSource imagesource, CancellationToken cancelationToken = default(CancellationToken), float scale = 1)
        {
            FormsCAKeyFrameAnimation animation = await ImageAnimationHelper.CreateAnimationFromUriImageSourceAsync(imagesource as UriImageSource, cancelationToken).ConfigureAwait(false);

            if (animation == null)
            {
                Forms.MauiContext?.CreateLogger <FileImageSourceHandler>()?.LogWarning("Could not find image: {imagesource}", imagesource);
            }

            return(animation);
        }
Ejemplo n.º 5
0
        public Task <FormsCAKeyFrameAnimation> LoadImageAnimationAsync(ImageSource imagesource, CancellationToken cancelationToken = default(CancellationToken), float scale = 1)
        {
            FormsCAKeyFrameAnimation animation = ImageAnimationHelper.CreateAnimationFromFileImageSource(imagesource as FileImageSource);

            if (animation == null)
            {
                Forms.MauiContext?.CreateLogger <FileImageSourceHandler>()?.LogWarning("Could not find image: {imagesource}", imagesource);
            }

            return(Task.FromResult(animation));
        }
Ejemplo n.º 6
0
        static public FormsCAKeyFrameAnimation CreateAnimationFromFileImageSource(FileImageSource imageSource)
        {
            FormsCAKeyFrameAnimation animation = null;
            string file = imageSource?.File;

            if (!string.IsNullOrEmpty(file))
            {
                using (var parsedImageSource = CGImageSource.FromUrl(NSUrl.CreateFileUrl(file, null)))
                {
                    animation = ImageAnimationHelper.CreateAnimationFromCGImageSource(parsedImageSource);
                }
            }

            return(animation);
        }
Ejemplo n.º 7
0
        protected override void Dispose(bool disposing)
        {
            if (_isDisposed)
            {
                return;
            }

            _isDisposed = true;

            if (disposing && _animation != null)
            {
                _animation.AnimationStopped -= OnAnimationStopped;
                Layer.RemoveAnimation(AnimationLayerName);
                _animation.Dispose();
                _animation = null;
            }

            base.Dispose(disposing);
        }
Ejemplo n.º 8
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);
        }